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 ride
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.