zoukankan      html  css  js  c++  java
  • jsonobject 遍历 org.json.JSONObject

    1. import org.json.JSONArray;  
    2. import org.json.JSONException;  
    3. import org.json.JSONObject;  
    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.       
    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"}]}  
  • 相关阅读:
    C# 枚举、字符串、值的相互转换
    What's New in v2010 vol 2.5
    Using Oracle's Parallel Execution Features
    [zhuan]asp.net程序性能优化的七个方面 (c#(或vb.net)程序改进)
    ORACLE常用网址
    html中的块元素(block element)和内联元素(inline element)
    软件构架师的特点
    窗体信息处理函数讲解
    [xue]软件项目经理所必需具备的素质
    Gulp系列文章入门Gulp
  • 原文地址:https://www.cnblogs.com/ldq2016/p/5780881.html
Copyright © 2011-2022 走看看