[HTML]View PlainCopy
- Import Org.json.JSONArray;
- Import org.json.JSONException;
- Import Org.json.JSONObject;
[Java]View PlainCopy
- Public static void Main (string[] args) {
- String str = "{' 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}]}";
- Jsonarray NewArray = new Jsonarray ();
- Jsonobject Newjson = new Jsonobject ();
- try {
- Jsonobject obj = new Jsonobject (str);
- Iterator it = Obj.keys ();
- While (It.hasnext ()) {
- String key = (string) it.next ();
- String value = obj.getstring (key);
- Jsonarray array = Obj.getjsonarray (key);
- For (int i=0;i<array.length (); i++) {
- Jsonobject Jsonobject = 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 (Jsonexception e) {
- E.printstacktrace ();
- }
- }
[Java]View PlainCopy
- {"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": "AB3"} , { 10, "name": " Aa1 "},{" exp ": " Ti=aa2 ", 15, "name": "value": Span class= "string" > "Aa2"},{ "exp": "Ti=aa3", 20," Value ": " Aa3 "}]}&NBSP;&NBSP;
Jsonobject Traversal Org.json.JSONObject