jsonobject 遍历 org.json.JSONObject
- import org.json.JSONArray;
- import org.json.JSONException;
- import org.json.JSONObject;
- public static void main(String[] args) {
- String str = "{'TI':[{'value':'aa1','count':10},{'value':'aa2','count':15},{'value':'aa3','count':20}]," +
- "'AB':[{'value':'ab','count':110},{'value':'ab2','count':115},{'value':'ab3','count':210}]}";
- JSONArray newArray = new JSONArray();
- JSONObject newJson = new JSONObject();
- try {
- JSONObject obj = new JSONObject(str);
- Iterator it = obj.keys();
- while (it.hasNext()) {
- String key = (String) it.next();
- String value = obj.getString(key);
- JSONArray array = obj.getJSONArray(key);
- for(int i=0;i<array.length();i++){
- JSONObject jsonobject = array.getJSONObject(i);
- jsonobject.put("name", key);
- jsonobject.put("exp", key+"="+jsonobject.getString("value"));
- newArray.put(jsonobject);
- }
- }
- newJson.put("groups",newArray);
- System.out.println(newJson);
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }
- {"groups":[{"exp":"AB=ab","count":110,"name":"AB","value":"ab"},{"exp":"AB=ab2","count":115,"name":"AB","value":"ab2"},{"exp":"AB=ab3","count":210,"name":"AB","value":"ab3"},{"exp":"TI=aa1","count":10,"name":"TI","value":"aa1"},{"exp":"TI=aa2","count":15,"name":"TI","value":"aa2"},{"exp":"TI=aa3","count":20,"name":"TI","value":"aa3"}]}
jsonobject 遍历 org.json.JSONObject的更多相关文章
- net.sf.json.JSONObject 和org.json.JSONObject 的差别
http://my.oschina.net/wangwu91/blog/340721 net.sf.json.JSONObject 和org.json.JSONObject 的差别. 一.创建jso ...
- net.sf.json.JSONObject 和org.json.JSONObject
参考 net.sf.json.JSONObject 和org.json.JSONObject 的差别
- JsonArray和JsonObject遍历方法
一:遍历JsonArray String str = "[{name:'a',value:'aa'},{name:'b',value:'bb'},{name:'c',value:'cc'}, ...
- 遍历jsonArray和jsonObject
遍历jsonArray String str = "[{name:'a',value:'aa'},{name:'b',value:'bb'},{name:'c',value:'cc'}]&q ...
- JSONObject遍历
导入JSONObject和JSONArray所需要的jar包 JSONObject所必需的6个jar包: commons-beanutils-1.7.0.jar commons-collections ...
- 使用JSONObject遇到的问题,java.lang.NoClassDefFoundError: net/sf/json/JSONObject
先是报 java.lang.NoClassDefFoundError: net/sf/json/JSONObject 这个错误, 打开项目属性找到java build path中的libaries,找 ...
- 大话 JSON 之 JSONObject.getString(“”) 方法 和 JSONObject.optString(“”) 的区别
运行以下代码: public static void main(String[] args) { JSONObject test = new JSONObject(); test.put(" ...
- java中转换json方式(JSONArray,JSONObject),json解析
package com.yunos.tv.video.resource.controller.web; import java.util.ArrayList; import java.util.Has ...
- org.json.JSONObject的getString和optString使用注意事项
结论:org.json.JSONObject的getString如果取不到对应的key会抛出异常,optString则不会 /** * Returns the value mapped by {@co ...
随机推荐
- UISearchController
搜索框UISearchController的使用(iOS8.0以后替代UISearchBar + UIS) 1.在iOS 8.0以上版本中, 我们可以使用UISearchController来非常方便 ...
- iOS关于CAShapeLayer与UIBezierPath的知识内容
使用CAShapeLayer与UIBezierPath可以实现不在view的drawRect方法中就画出一些想要的图形 . 1:UIBezierPath: UIBezierPath是在 UIKit 中 ...
- 深入.net(数据类型)
C#究竟为我们提供了哪些“数据类型”供我们使用?这些类型有什么样的“特征”? 数据类型的分类: --- 数据类型是存放数据的容器.那么我们就以它们“存放数据的方式”分类! 1.值类型:变量中直接存放着 ...
- 【读书笔记】iOS-ARC-Xcode检测循环引用
一,在桌面上新建立一个工程,在ViewController.m中输入如下代码: - (void)viewDidLoad { [super viewDidLoad]; // Do any additio ...
- OC 内存泄露 自动释放池
花絮:看到下面的代码就想起这么一个调侃: 一个老程序员,功成名就,金盆洗手不在写代码后,决定练练书法.提笔思索良久后在纸上写下:Hello world! /********************** ...
- 网络热恋之XML解析
XML 可扩展标记语言 用于标记电子文件使其具有结构性的标记语言,可以用来标记数据.定义数据类型,是一种允许用户对自己的标记语言进行定义的源语言 易读性高,编码手写难度小,数据量大 NSXMLPars ...
- request,session,application
JSP 的3个内置对象request,session,application,其实都有一个作用域,这些对象内部有一个Map成员用于存放数据,比如session对象的setAttribute(key,v ...
- Python基础-day2
1.Python模块python 中导入模块使用import语法格式:import module_name示例1: 导入os模块system('dir')列出当前目录下的所有文件 # _*_ codi ...
- Windows10 利用 Docker 配置 TensofFlow 深度学习工具
TensorFlow 这个不用多介绍了吧,大家都知道,Google的开源深度学习软件库,官网点这里:https://www.tensorflow.org/ 当然这个工具官方支持装在 Ubuntu 和 ...
- android oncreate获取宽高度
gridView = (GridView) getView().findViewById(R.id.gridView_musicbook); gridView.getViewTreeObserver( ...