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
🌐
Stleary
stleary.github.io › JSON-java › org › json › JSONArray.html
JSONArray
The internal form is an object ... and put methods for adding or replacing values. The values can be any of these types: Boolean, JSONArray, JSONObject, Number, String, or the JSONObject.NULL object. The constructor can convert a JSON text into a Java object....
🌐
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, ...
🌐
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[]) ...
🌐
Couchbase
docs.couchbase.com › sdk-api › couchbase-java-client-2.0.0 › com › couchbase › client › java › document › json › JsonArray.html
JsonArray (java-client 2.0.0 API)
public java.lang.Object get(int index) Retrieves the value by the position in the JsonArray and does not cast it. Parameters: index - the index of the value. Returns: the value if found, or null otherwise. public JsonArray add(java.lang.Object value) Append an element to the JsonArray.
🌐
Tabnine
tabnine.com › home page › code › java › org.json.jsonarray
org.json.JSONArray.put java code examples | Tabnine
cookieJSONArray = new JSONArray(value); if (null == cookieJSONArray || 0 == cookieJSONArray.length()) { return false; for (int j = 0; j < cookieJSONArray.length(); j++) { final String visitedURL = cookieJSONArray.optString(j); response.addCookie(c); } else if (needToAppend) { cookieJSONArray.put(request.getRequestURI()); final Cookie c = new Cookie("visited", URLEncoder.encode(cookieJSONArray.toString(), "UTF-8")); c.setMaxAge(COOKIE_EXPIRY); c.setPath("/");
🌐
SourceForge
json-lib.sourceforge.net › apidocs › net › sf › json › JSONArray.html
JSONArray (Overview (json-lib jdk 1.3 API))
The internal form is an object ... and element methods for adding or replacing values. The values can be any of these types: Boolean, JSONArray, JSONObject, Number, String, or the JSONNull object. The constructor can convert a JSON text into a Java object....
Find elsewhere
🌐
TutorialsPoint
tutorialspoint.com › how-to-write-create-a-json-array-using-java
How to Write/create a JSON array using Java?
September 6, 2023 - JSONArray array = new JSONArray(); array.add("element_1"); array.add("element_2"); array.add("element_3"); After adding all the required elements add the array into the JSON document using the put() method as ? jsonObject.put("contact",array); Write the created JSON object into a file using ...
🌐
Oracle
docs.oracle.com › javaee › 7 › api › javax › json › JsonArrayBuilder.html
JsonArrayBuilder (Java(TM) EE 7 Specification APIs)
JsonBuilderFactory factory = Json.createBuilderFactory(config); JsonArray value = factory.createArrayBuilder() .add(factory.createObjectBuilder() .add("type", "home") .add("number", "212 555-1234")) .add(factory.createObjectBuilder() .add("type", "fax") .add("number", "646 555-4567")) .build();
🌐
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); } }
🌐
Oracle
docs.oracle.com › en › middleware › maf › 2.5.1 › reference-java-api › oracle › adfmf › json › JSONArray.html
JSONArray (Oracle Fusion Middleware Java API Reference for Oracle Mobile Application Framework)
A JSONArray is an ordered sequence of values. Its external text form is a string wrapped in square brackets with commas separating the values. The internal form is an object having get and opt methods for accessing the values by index, and put methods for adding ...
🌐
Progress
documentation.progress.com › output › ua › OpenEdge_latest › dvref › add(-)-method-(jsonarray).html
Add( ) method (JsonArray)
Indexing into JsonArrays is 1-based. For example, myArray:Add(5, "custnum") inserts a String value "custnum" as element 6 into the JsonArray.
🌐
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,…