Have you tried using JSONArray.getJSONObject(int), and JSONArray.length() to create your for-loop:
for (int i = 0; i < recs.length(); ++i) {
JSONObject rec = recs.getJSONObject(i);
int id = rec.getInt("id");
String loc = rec.getString("loc");
// ...
}
Answer from notnoop on Stack OverflowOracle
docs.oracle.com › javaee › 7 › api › javax › json › JsonArray.html
JsonArray (Java(TM) EE 7 Specification APIs)
JsonArray represents an immutable JSON array (an ordered sequence of zero or more values).
Stleary
stleary.github.io › JSON-java › org › json › JSONArray.html
JSONArray
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 or replacing values.
Videos
04:24
What Is A JSON Array? - Next LVL Programming - YouTube
14:44
Create Complex Json in Java | Json Object | Json Array In Java ...
02:08
JSON Arrays Explained: Beginner's Guide | JSON Tutorial - YouTube
06:29
How to fix "Value of type org.json.JSONArray cannot be converted ...
17:18
How to Parse JSON Array in Android Studio || JSONArray || Foysal ...
13:28
27. Understand and Create JSON Array and Nested JSON Array - YouTube
Top answer 1 of 6
245
Have you tried using JSONArray.getJSONObject(int), and JSONArray.length() to create your for-loop:
for (int i = 0; i < recs.length(); ++i) {
JSONObject rec = recs.getJSONObject(i);
int id = rec.getInt("id");
String loc = rec.getString("loc");
// ...
}
2 of 6
5
An org.json.JSONArray is not iterable.
Here's how I process elements in a net.sf.json.JSONArray:
JSONArray lineItems = jsonObject.getJSONArray("lineItems");
for (Object o : lineItems) {
JSONObject jsonLineItem = (JSONObject) o;
String key = jsonLineItem.getString("key");
String value = jsonLineItem.getString("value");
...
}
Works great... :)
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › system.text.json.nodes.jsonarray
JsonArray Class (System.Text.Json.Nodes) | Microsoft Learn
public ref class JsonArray sealed : System::Text::Json::Nodes::JsonNode, System::Collections::Generic::ICollection<System::Text::Json::Nodes::JsonNode ^>, System::Collections::Generic::IEnumerable<System::Text::Json::Nodes::JsonNode ^>, System::Collections::Generic::IList<System::Text::Json::Nodes::JsonNode ^>
W3Schools
w3schools.com › js › js_json_arrays.asp
W3Schools.com
Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, Python, PHP, Bootstrap, Java, XML and more.
Android Developers
developer.android.com › api reference › jsonarray
JSONArray | API reference | Android Developers
Skip to main content · English · Deutsch · Español – América Latina · Français · Indonesia · Polski · Português – Brasil · Tiếng Việt · 中文 – 简体
Esri Developer
developers.arcgis.com › enterprise-sdk › api-reference › java › com › esri › arcgis › server › json › JSONArray.html
JSONArray (ArcObjects Java API)
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 or replacing values.
Top answer 1 of 3
14
But how can I create a JSONArray and insert it to the JSONObject?
You can create JSONArray same like you have tried to create JSONObject.
Creating time:
For example:
JSONArray myArray = new JSONArray();
JSONObject j = new JSONObject();
j.put("key",value);
j.put("array",myArray);
Retrieving time:
you can fetch the value of String or JSONObject or any by their key name. For example:
JSONArray myArray = objJson.getJSONArray("array");
2 of 3
7
You can do it like:
String[] data = {"stringone", "stringtwo"};
JSONArray json = new JSONArray(Arrays.toString(data));
Or, create a JSONArray object and use the put method(s) to add any Strings you want. To output the result, just use the toString() method.
Javadoc.io
javadoc.io › doc › com.google.code.gson › gson › 2.6.2 › com › google › gson › JsonArray.html
JsonArray - gson 2.6.2 javadoc
Bookmarks · Latest version of com.google.code.gson:gson · https://javadoc.io/doc/com.google.code.gson/gson · Current version 2.6.2 · https://javadoc.io/doc/com.google.code.gson/gson/2.6.2 · package-list path (used for javadoc generation -link option) · https://javadoc.io/doc/com.goog...
ArduinoJson
arduinojson.org › version 7 › api › jsonarray
JsonArray | ArduinoJson 7
ArduinoJson 7. A JSON array is an ordered collection of values. A JsonArray is a reference to this array, but a JsonDocument owns the data.
ArduinoJson
arduinojson.org › version 6 › api › jsonarray
JsonArray | ArduinoJson 6
ArduinoJson 6 user’s manual. A JSON array is an ordered collection of values. A JsonArray is a reference to this array, but a JsonDocument owns the data.
Eclipse Vert.x
vertx.io › docs › apidocs › io › vertx › core › json › JsonArray.html
JsonArray (Vert.x Stack - Docs 5.0.7 API)
Create a JsonArray containing an arbitrary number of values.
Javadoc.io
javadoc.io › doc › com.google.code.gson › gson › 2.8.5 › com › google › gson › JsonArray.html
JsonArray - gson 2.8.5 javadoc
Bookmarks · Latest version of com.google.code.gson:gson · https://javadoc.io/doc/com.google.code.gson/gson · Current version 2.8.5 · https://javadoc.io/doc/com.google.code.gson/gson/2.8.5 · package-list path (used for javadoc generation -link option) · https://javadoc.io/doc/com.goog...
Micro Focus
microfocus.com › documentation › silk-performer › 205 › en › silkperformer-205-webhelp-en › GUID-0847DE13-2A2F-44F2-A6E7-214CD703BF84.html
JSON Array Structure
Describes what a JSON array is, how it is structured, and what data types are allowed in a JSON array.
Maven Repository
mvnrepository.com › artifact › org.json › json
Maven Repository: org.json » json
December 24, 2025 - JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write · JSON is a light-weight, language independent, data interchange format. See http://www.JSON.org/ The files in this package implement JSON encoders/decoders in Java.
Oracle
docs.oracle.com › middleware › maf250 › mobile › api-ref › 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 or replacing values.
JSON Schema
json-schema.org › understanding-json-schema › reference › array
JSON Schema - array
Arrays are used for ordered elements. In JSON, each element in an array may be of a different type · In Python, "array" is analogous to the list or tuple type, depending on usage. However, the json module in the Python standard library will always use Python lists to represent JSON arrays