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.

Answer from mellamokb on Stack Overflow
🌐
TutorialsPoint
tutorialspoint.com › how-can-we-add-a-jsonarray-to-jsonobject-in-java
How can we add a JSONArray to JSONObject in Java?
July 4, 2020 - We need to add a few items to an ArrayList first and pass this list to the put() method of JSONArray class and finally add this array to JSONObject using the put() method. import org.json.*; import java.util.*; public class AddJSONArrayToJSONObjTest { public static void main(String args[]) ...
🌐
Oracle
docs.oracle.com › javaee › 7 › api › javax › json › JsonArray.html
JsonArray (Java(TM) EE 7 Specification APIs)
JsonArray instances are list objects that provide read-only access to the values in the JSON array. Any attempt to modify the list, whether directly or using its collection views, results in an UnsupportedOperationException. ... add, add, addAll, addAll, clear, contains, containsAll, equals, ...
🌐
Tabnine
tabnine.com › home page › code › java › org.json.jsonarray
org.json.JSONArray.put java code examples | Tabnine
private JSONArray convertListToJsonArray(Object value) throws InvocationTargetException, IllegalAccessException { JSONArray array = new JSONArray(); List<Object> list = (List<Object>) value; for(Object obj : list) { // Send null, if this is an array of arrays we are screwed array.put(obj != null ?
🌐
TutorialsPoint
tutorialspoint.com › how-to-write-create-a-json-array-using-java
How to Write/create a JSON array using Java?
September 6, 2023 - //Creating a JSONObject object ... class. jsonObject.put("key", "value"); Create a JSON array by instantiating the JSONArray class and add, elements to the created array using the add() method of the JSONArray class....
🌐
Stleary
stleary.github.io › JSON-java › org › json › JSONArray.html
JSONArray
Put or replace an object value in the JSONArray. If the index is greater than the length of the JSONArray, then null elements will be added as necessary to pad it out. ... value - The value to put into the array.
Find elsewhere
🌐
Processing
processing.github.io › processing-javadocs › core › processing › data › JSONArray.html
JSONArray
Append a boolean value. This increases the array's length by one. ... Put or replace a String value. If the index is greater than the length of the JSONArray, then null elements will be added as necessary to pad it out.
🌐
TutorialsPoint
tutorialspoint.com › how-can-we-add-a-jsonarray-within-jsonobject-in-java
How can we add a JSONArray within JSONObject in Java?
import org.json.*; public class AddJSONArrayTest { public static void main(String[] args) throws JSONException { JSONArray array = new JSONArray(); array.put("INDIA"); array.put("AUSTRALIA"); array.put("ENGLAND"); JSONObject obj = new JSONObject(); obj.put("COUNTRIES", array); System.out.println(obj); } }
🌐
Tabnine
tabnine.com › home page › code › java › com.google.gson.jsonarray
com.google.gson.JsonArray.add java code examples | Tabnine
@Override public void userDelete(String[] userids) throws WxErrorException { String url = "https://qyapi.weixin.qq.com/cgi-bin/user/batchdelete"; JsonObject jsonObject = new JsonObject(); JsonArray jsonArray = new JsonArray(); for (int i = 0; i < userids.length; i++) { jsonArray.add(new JsonPrimitive(userids[i])); } jsonObject.add("useridlist", jsonArray); post(url, jsonObject.toString()); } ... @Override public PersistedData create(boolean... values) { JsonArray array = new JsonArray(); for (boolean val : values) { array.add(new JsonPrimitive(val)); } return new GsonPersistedData(array); }
🌐
Processing
processing.org › reference › JSONArray_append_.html
JSONArray - append() / Reference / Processing.org
January 1, 2021 - Appends a new value to the JSONArray, increasing the array's length by one. New values may be of the following types: int, float, String, boolean, JSONObject,…
🌐
Stack Overflow
stackoverflow.com › questions › 34854560 › java-jsonobject-adding-a-new-element-in-arrays-jsonarray
JAVA JSONObject adding a new element in Arrays (JSONArray) - Stack Overflow
import org.codehaus.jettison.json.JSONArray; import org.codehaus.jettison.json.JSONException; import org.codehaus.jettison.json.JSONObject; import org.junit.Before; import org.junit.Test; public class JettisonJSONOperations { JSONObject jsonObject = new JSONObject(); @Before public void preCreateJsonObject (){ try { jsonObject.put("groupName","administrators"); JSONArray members = new JSONArray(); members.put("edward"); members.put("richard"); members.put("john"); jsonObject.put("members", members); } catch (JSONException e) { e.printStackTrace(); } System.out.println(jsonObject.toString()); } @Test public void addingElementToJSONArray (){ try { jsonObject.append("members", "batman"); } catch (JSONException e) { e.printStackTrace(); } System.out.println(jsonObject.toString()); } }
🌐
Couchbase
docs.couchbase.com › sdk-api › couchbase-java-client › com › couchbase › client › java › json › JsonArray.html
JsonArray (Couchbase Java SDK 3.11.1 API)
Static method to create a JsonArray from a JSON String. Not to be confused with from(Object...) from(aString)} which will populate a new array with the string. The string is expected to be a valid JSON array representation (eg.
🌐
Stack Overflow
stackoverflow.com › questions › 50039189 › how-to-add-object-to-jsonarray
java - How to add object to JSONArray - Stack Overflow
April 26, 2018 - public class Test { public static void main(String[] args) { Mypojo mypojo = new Mypojo(); Gson gson = new Gson(); JSONArray records = new JSONArray(); for (int i = 0; i < 1; i++) { if (5 > 0) { mypojo.setPoAccount("050017"); mypojo.setPoAmount("12"); JSONObject objects = new JSONObject(gson.toJson(mypojo)); records.put(objects); } mypojo.setPoAccount("050016"); mypojo.setPoAmount("800"); JSONObject objects = new JSONObject(gson.toJson(mypojo)); records.put(objects); } System.out.println(records); } }
🌐
Tabnine
tabnine.com › home page › code › java › org.json.jsonobject
org.json.JSONObject.append java code examples | Tabnine
July 11, 2021 - private JSONObject addInclude(JSONObject json, String include) throws JSONException { return json.append("includes", include); } ... JSONObject result = new JSONObject(); JSONArray array1 = new JSONArray(); for (List<Map<String, String>> tmpList : finalOutput) { JSONArray array2 = new JSONArray(); for (int i = 0; i < tmpList.size(); i++) { Map<String, String> insideMap = tmpList.get(i); JSONObject obj = new JSONObject(insideMap); array2.add(obj); } array1.add(array2); } result.append("result", array1); return result.toString();