java - JSONObject inside JSONArray -


i'm trying put jsonobject inside jsonarray in java. here 2 objects:

jsonarray:

[{     "url": null,     "flag": "0",     "read": "0",     "time": 2000,     "exp": null,     "population": 10 }] 

jsonobject:

{ "events": [     {         "color": "green",         "event": "restart"     },     {         "color": "black",         "event": "shutdown"     },     {         "color": "white",         "event": "read"     }        ] } 

expected result:

[ {     "url": null,     "flag": "0",     "read": "0",     "time": 2000,     "exp": null,     "population": 10,     "events": [         {             "color": "green",             "event": "restart"         },         {             "color": "black",             "event": "shutdown"         },         {             "color": "white",             "event": "read"         }     ] } ] 

i tried use code, result not ok:

jsonarray.put(jsonobject); 

unexpected result:

[ {     "url": null,     "flag": "0",     "read": "0",     "time": 2000,     "exp": null,     "population": 10 }, {     "events": [         {             "color": "green",             "event": "restart"         },         {             "color": "black",             "event": "shutdown"         },         {             "color": "white",             "event": "read"         }     ] } ] 

the "events" key-value inside unique element in jsonarray, not element.

the jsonarray contains 1 jsonobject. when jsonarray.put(jsonobject); adding jsonarray, not jsonobject in jsonarray.

this add jsonobject first jsonobject in jsonarray

jsonarray.getjsonobject(0).put("events",jsonobject.get("events")); 

Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -