jsonobject 遍历 org.json.JSONObject

简介:
[html]  view plain  copy
  1. import org.json.JSONArray;  
  2. import org.json.JSONException;  
  3. import org.json.JSONObject;  

 

[java]  view plain  copy
  1. public static void main(String[] args) {  
  2.         String str = "{'TI':[{'value':'aa1','count':10},{'value':'aa2','count':15},{'value':'aa3','count':20}]," +  
  3.                 "'AB':[{'value':'ab','count':110},{'value':'ab2','count':115},{'value':'ab3','count':210}]}";  
  4.         JSONArray newArray = new JSONArray();  
  5.         JSONObject newJson = new JSONObject();  
  6.         try {  
  7.             JSONObject obj = new JSONObject(str);  
  8.             Iterator it = obj.keys();  
  9.             while (it.hasNext()) {  
  10.                 String key = (String) it.next();  
  11.                 String value = obj.getString(key);  
  12.                 JSONArray array = obj.getJSONArray(key);  
  13.                 for(int i=0;i<array.length();i++){  
  14.                     JSONObject jsonobject = array.getJSONObject(i);  
  15.                     jsonobject.put("name", key);  
  16.                     jsonobject.put("exp", key+"="+jsonobject.getString("value"));  
  17.                     newArray.put(jsonobject);  
  18.                 }  
  19.             }  
  20.             newJson.put("groups",newArray);  
  21.             System.out.println(newJson);  
  22.         } catch (JSONException e) {  
  23.             e.printStackTrace();  
  24.         }  
  25.     }  
  26.       
[java]  view plain  copy
  1. {"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"}]}  

 


    本文转自 一点点征服   博客园博客,原文链接:http://www.cnblogs.com/ldq2016/p/5780881.html,如需转载请自行联系原作者

相关文章
|
XML 存储 JSON
JSON、JSONObject 与 JSONArray 详细介绍及其应用方式
JSON、JSONObject 与 JSONArray 详细介绍及其应用方式
1271 1
JSON、JSONObject 与 JSONArray 详细介绍及其应用方式
|
JSON 数据格式
net.sf.json.JSONObject 和org.json.JSONObject 的差别
net.sf.json.JSONObject 和org.json.JSONObject 的差别
|
6天前
|
存储 JSON Java
JSON.toJSONString什么作用
JSON.toJSONString什么作用
|
缓存 fastjson Java
FastJson - JSONPath 使用
FastJson - JSONPath 使用
1289 0
|
JSON 缓存 JavaScript
Jayway - Json-Path 使用(二)
Jayway - Json-Path 使用(二)
851 0
|
JSON fastjson 数据格式
根据json的key获取到对应的value
根据json的key获取到对应的value
887 0
|
JSON fastjson 数据格式
利用fastjson对json转map的操作
String str = "{\"0\":\"zhangsan\",\"1\":\"lisi\",\"2\":\"wangwu\",\"3\":\"maliu\"}"; //第一种方式 Map maps = (Map)JSON.
10942 1
|
JSON Java 数据格式
Json string to object debug - json字符串转Java对象的处理调试
Json string to object debug - json字符串转Java对象的处理调试
125 0
Json string to object debug - json字符串转Java对象的处理调试
|
JSON 数据格式
JsonArray和JsonObject遍历方法
一:遍历JsonArray // 一个未转化的字符串 String str = "[{name:'a',value:'aa'},{name:'b',value:'bb'},{name:'c',value:'cc'},{name:'d',value:'d...
6863 0
|
JSON 数据格式