jsonobject 遍曆 org.json.JSONObject

來源:互聯網
上載者:User

標籤:

[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"}]}  

 

jsonobject 遍曆 org.json.JSONObject

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.