简体   繁体   中英

How to convert JSON Object to JSON Array

Hi I have a JSON Object contains data Like

{"THIRD":"OK","SECOND":"OK","FIRST":"OK"}
{"THIRD":"OK","SECOND":"NULL","FIRST":"OK"}
{"THIRD":"OK","SECOND":"OK","FIRST":"OK"}

When I tried to add this JSON object to JSON array it is only taking last one in JSON Object({"THIRD":"OK","SECOND":"OK","FIRST":"OK"}).Can any one help me in solving this.

I am using it like

JSONObject object = new JSONObject();
JSONArray array = new JSONArray();
array.add(object);(Only adding last element in JSON Object)

Instead of taking all values in the JSON Object Json Array is only taking last value in JSON Object.

Here is solution:

JSONArray array = new JSONArray();
for(int i = 0; i < 3; i++)
{
     JSONObject sec=new JSONObject();
     sec.put("THIRD","OK");
     sec.put("SECOND","OK");
     sec.put("FIRST","OK");
     array.put(sec);
}

You have to make JSONObject every time then add it to JSONArray

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM