Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I turn a JSONArray into a JSONObject?

Basically I have:

JSONArray j = new JSONArray();
j.add(new JSONObject()); //JSONObject has a bunch of data in it
j.add(new JSONArray());  //JSONArray has a bunch of data in it

And now I would like to turn that JSONArray into a JSONObject with the same information in it. So that I can pass around the Object and then when I want I can grab all the information out of the object. Any help would be appreciated, Thanks.

like image 544
Grammin Avatar asked Aug 25 '11 16:08

Grammin


1 Answers

Typically, a Json object would contain your values (including arrays) as named fields within. So, something like:

JSONObject jo = new JSONObject();
JSONArray ja = new JSONArray();
// populate the array
jo.put("arrayName",ja);

Which in JSON will be {"arrayName":[...]}.

like image 73
david van brink Avatar answered Oct 04 '22 11:10

david van brink