jsonobject 遍历 org.json.JSONObject

前端之家收集整理的这篇文章主要介绍了jsonobject 遍历 org.json.JSONObject前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
import org.json.JSONArray;
import org.json.JSONException;

import org.json.JSONObject;



  1. publicstaticvoidmain(String[]args){
  2. Stringstr="{'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. JSONArraynewArray=newJSONArray();
  5. JSONObjectnewJson=newJSONObject();
  6. try{
  7. JSONObjectobj=newJSONObject(str);
  8. Iteratorit=obj.keys();
  9. while(it.hasNext()){
  10. Stringkey=(String)it.next();
  11. Stringvalue=obj.getString(key);
  12. JSONArrayarray=obj.getJSONArray(key);
  13. for(inti=0;i<array.length();i++){
  14. JSONObjectjsonobject=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(JSONExceptione){
  23. e.printStackTrace();
[java] view plain copy
    {"groups":[{"exp":"AB=ab","count":110,"name":"AB","value":"ab"},{"exp":"AB=ab2",0); background-color:inherit">115,"value":"ab2"},{"exp":"AB=ab3",0); background-color:inherit">210,"value":"ab3"},{"exp":"TI=aa1",0); background-color:inherit">10,"name":"TI","value":"aa1"},{"exp":"TI=aa2",0); background-color:inherit">15,"value":"aa2"},{"exp":"TI=aa3",0); background-color:inherit">20,"value":"aa3"}]}

猜你在找的Json相关文章