0

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.

vicky
  • 45
  • 8

1 Answers1

0

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

Son Truong
  • 13,661
  • 5
  • 32
  • 58