import org.json.JSONException;
import org.json.JSONObject;
- publicstaticvoidmain(String[]args){
- Stringstr="{'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}]}";
- JSONArraynewArray=newJSONArray();
- JSONObjectnewJson=newJSONObject();
- try{
- JSONObjectobj=newJSONObject(str);
- Iteratorit=obj.keys();
- while(it.hasNext()){
- Stringkey=(String)it.next();
- Stringvalue=obj.getString(key);
- JSONArrayarray=obj.getJSONArray(key);
- for(inti=0;i<array.length();i++){
- JSONObjectjsonobject=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(JSONExceptione){
- e.printStackTrace();
- {"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"}]}