10

Hi All I am not the best at Json. I was trying to add some json object into a json array through a loop, but the problem is everytime it comes to the loop, it also over rides the previous data in the array by the new data. here is my code:

JSONObject jsonObj = new JSONObject();
JSONArray jsonArray = new JSONArray();
if(X.size() > 0)
{
  for (int j = 0; j < X.size(); j++)
   {
    zBean aBean = (zBean)X.get(j);
    jsonObj.put(ID,newInteger(aBean.getId()));
    jsonObj.put(NAME,aBean.getName());
    jsonArray.add(jsonObj);
   }
}

example given X.size = 2:

when j=0
jsonObj => {"Name":"name1","Id":1000}
jsonArray => [{"Name":"name1","Id":1000}]

when j = 1
jsonObj => {"Name":"name2","Id":1001}
jsonArray => [{"Name":"name2","Id":1001},{"Name":"name2","Id":1001}]

I hope my example is clear enough.

Id be grateful if anyone can help me here.

3 Answers 3

26

You need to create a new jsonObj reference with every iteration of the loop:

for (int j = 0; j < X.size(); j++)
 {
  zBean aBean = (zBean)X.get(j);
  jsonObj = new JSONObject();
//^^^^^^^^^^^^^^^^^^^^^^^^^^^ add this line
  jsonObj.put(ID,newInteger(aBean.getId()));
  jsonObj.put(NAME,aBean.getName());
  jsonArray.add(jsonObj);
 }

Otherwise you are updating the same instance over and over again, and adding a reference to the same object many times to the array. Since they are all the same reference, a change to one of them affects all of them in the array.

Sign up to request clarification or add additional context in comments.

Comments

2

Following will add json obj into json array

public static void main(String[] args) {
    JSONArray jsonArray = new JSONArray();
    int i = 0;
    while(i < 3)
    {
        JSONObject jsonObj = new JSONObject();
        jsonObj.put("Name","Random"+i);
        jsonObj.put("ID", i);
        jsonArray.put(jsonObj); //jsonObj will be pushed into jsonArray
        i++;
    }
    System.out.println("jsonArray : "+ jsonArray);
}

Output:

jsonArray : [{"ID":0,"Name":"Random0"},{"ID":1,"Name":"Random1"},{"ID":2,"Name":"Random2"}]

.pom has following dependency

<dependencies>
    <dependency>
        <groupId>org.json</groupId>
        <artifactId>json</artifactId>
        <version>20180813</version>
    </dependency>
    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.8.6</version>
    </dependency>
</dependencies>

Comments

0

Although I created a new JSONObject every time the loop ran, I still had the same problem. so what I did is I created a List of JSONObjects and each time the loop ran I added a new record to my list and update that record each time. At the end in a for-loop, I fed my JSONArray by my list.

JSONArray jsonArray = new JSONArray();
List<JSONObject> myList = new ArrayList()<>;
if(X.size() > 0)
{
  for (int j = 0; j < X.size(); j++)
  {
    myList.add(new JSONObject());
    zBean aBean = (zBean)X.get(
    myList.get(j).put(ID,newInteger(aBean.getId()));
    myList.get(j).put(NAME,aBean.
  }
  for(int j = 0; j < myList.size(); j++)
    jsonArray.add(myList.get(j));
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.