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 Overflow
Discussions

How do you get a specific value in json text in java?
Hello everyone, How do I get a specific value in json text in java ? Example - Like json text is - { "first" : "1st", "second" : "2nd" } So, how do I get the value for second ? More on community.appinventor.mit.edu
🌐 community.appinventor.mit.edu
0
0
July 29, 2021
New to Java, wanted some help with reading .json object arrays.
it gives me an error What's the error? More on reddit.com
🌐 r/javahelp
11
1
February 25, 2024
java - How to Get JSON Array Within JSON Object? - Stack Overflow
Communities for your favorite technologies. Explore all Collectives · Ask questions, find answers and collaborate at work with Stack Overflow for Teams More on stackoverflow.com
🌐 stackoverflow.com
[Java] How can I parse a JSON string then extract values from it?
One would think you'd use the same API you're already using, the one that has JSONArray in it. Are you unable to find or understand the docs for it? Do you understand the format of the data you're getting back? Are you otherwise competent with programming in Java, or are you still unable to do small projects on your own? Do you want pseudocode or real code spoonfed to you, or an algorithm outlined for you, or just some hints about how to do it? The data you get back looks like it might have multiple pieces of data in it, so can you attempt to use a loop here somehow? Do you understand JSON? More on reddit.com
🌐 r/learnprogramming
8
1
July 20, 2015
🌐
TutorialsPoint
tutorialspoint.com › how-to-read-parse-json-array-using-java
How to read/parse JSON array using Java?
May 5, 2025 - The iterator() method of the JSONArray class returns an Iterator object, using which you can iterate through the contents of the current array. Iterator<String> iterator = jsonArray.iterator(); while(iterator.hasNext()) { System.out.println(iterator.next()); } Below is an example of a Java ...
🌐
Baeldung
baeldung.com › home › json › get a value by key in a jsonarray
Get a Value by Key in a JSONArray | Baeldung
January 8, 2024 - Learn how to parse a JSONArray to get all the mapped values for a given key.
🌐
Tabnine
tabnine.com › home page › code › java › org.json.jsonarray
org.json.JSONArray.get java code examples | Tabnine
JSONObject json = new JSONObject(jsonString); String valueType = json.getString(JSON_VALUE_TYPE); JSONArray jsonArray = json.getJSONArray(JSON_VALUE); boolean[] array = new boolean[jsonArray.length()]; for (int i = 0; i < array.length; i++) { array[i] = jsonArray.getBoolean(i); String charString = json.getString(JSON_VALUE); if (charString != null && charString.length() == 1) { bundle.putChar(key, charString.charAt(0)); bundle.putString(key, json.getString(JSON_VALUE)); } else if (valueType.equals(TYPE_STRING_LIST)) { JSONArray jsonArray = json.getJSONArray(JSON_VALUE); int numStrings = jsonArray.length(); ArrayList<String> stringList = new ArrayList<String>(numStrings); for (int i = 0; i < numStrings; i++) { Object jsonStringValue = jsonArray.get(i); stringList.add( i,
🌐
Edureka Community
edureka.co › home › community › categories › java › accessing json array with java
Accessing Json array with Java | Edureka Community
January 22, 2019 - I'm just getting started with using json with java. For instance, my json looks like this: { "locations ... "id" and "loc" values within a for loop.
🌐
Coderanch
coderanch.com › t › 744956 › java › key-JSONArray
Get key:value from JSONArray (Java in General forum at Coderanch)
Take values from JSON file, and store that values into ArrayList, then sort it. The problem is that my json is array, and i do not know how to get just one pair, what is wrong with my code, can you help me?Please.
Find elsewhere
🌐
Oracle
docs.oracle.com › javaee › 7 › api › javax › json › JsonArray.html
JsonArray (Java(TM) EE 7 Specification APIs)
This is a convenience method for (JsonArray)get(index). ... Returns the number value at the specified position in this array.
🌐
MIT App Inventor
community.appinventor.mit.edu › open source development
How do you get a specific value in json text in java? - Open Source Development - MIT App Inventor Community
July 29, 2021 - Hello everyone, How do I get a specific value in json text in java ? Example - Like json text is - { "first" : "1st", "second" : "2nd" } So, how do I get the value for second ?
🌐
Stleary
stleary.github.io › JSON-java › org › json › JSONArray.html
JSONArray
Determine if the value is null. ... Make a string from the contents of this JSONArray. The separator string is inserted between each element. Warning: This method assumes that the data structure is acyclical. ... JSONException - If the array contains an invalid number.
🌐
Reddit
reddit.com › r/javahelp › new to java, wanted some help with reading .json object arrays.
r/javahelp on Reddit: New to Java, wanted some help with reading .json object arrays.
February 25, 2024 -
// Create a JSON object from the response
            JSONObject jsonResponse = new JSONObject(response.toString());

            // Write the JSON object to a file
            try (BufferedWriter writer = new BufferedWriter(new FileWriter("output.json"))) {
                writer.write(jsonResponse.toString(4)); // The number specifies the indentation for pretty printing
            }

            System.out.println("JSON response written to output.json");
        } else {
            System.out.println("HTTP request failed with response code: " + responseCode);
        }
        // Read the JSON data from the file
        String jsonContent = new String(Files.readAllBytes(Paths.get("output.json")));

        // Create a JSONObject from the JSON data
        JSONObject jsonObject = new JSONObject(jsonContent);
        JSONArray result = jsonObject.getJSONArray("result");
        JSONObject skey = result.getJSONObject(5);
        String memo = skey.getString("memo");
        System.out.println("Value of 'key' in the JSON object: " + memo);
        // Close the connection
        connection.disconnect();

Code ^^

i am trying to read a specific sub-key of a .json array which is saved from an http GET request.
the structure of the .json file is something like this:

"jsonarray": [{
    "subkey1": "a"
    "subkey2": "b"
    "subkey3": "c"
    "subkey4": "d"
    "subkey5": "e"
    "subkey6": "f"
    "subkey7": "g"
}],
"key1": "h",
"key2": "i"

i want to read the value of subkey5 i.e. "e", but when i try to do it, it gives me an error. where am i going wrong?

🌐
Crunchify
crunchify.com › json tutorials › how to parse jsonobject and jsonarrays in java? beginner’s guide
How to Parse JSONObject and JSONArrays in Java? Beginner's Guide • Crunchify
February 2, 2023 - Then, we use the getString, getInt, and getBoolean methods to extract values from the jsonObject. After printing out the values, we do a similar process to parse a jsonArrayString into a JSONArray and loop through each JSON object to extract ...
🌐
Processing
processing.org › reference › jsonarray_getjsonarray_
getJSONArray() / Reference
// // [ // [ // { "name": "apple", ... { json = loadJSONArray("data.json"); // Get the first array of elements JSONArray values = json.getJSONArray(0); for (int i = 0; i < values.size(); i++) { JSONObject item = values.getJSON...
🌐
Coderanch
coderanch.com › t › 694185 › open-source › reading-JSON-array
Help reading JSON array [Solved] (Open Source Projects forum at Coderanch)
May 15, 2018 - I think jsonObject.get() is returning an JSONArray which contains an array of Long objects.
🌐
Medium
medium.com › @Mohd_Aamir_17 › mastering-json-in-java-a-comprehensive-guide-to-handling-json-objects-arrays-and-nodes-with-df57bf0ebff1
Mastering JSON in Java: A Comprehensive Guide to Handling JSON Objects, Arrays, and Nodes with Jackson | by Mohd Aamir | Medium
November 4, 2024 - Whether you’re developing REST APIs, handling data transformation, or integrating with third-party services, understanding how to handle JSON data efficiently in Java is crucial. This article offers an in-depth look at how to parse, create, and manipulate JSON objects, arrays, and nodes using the Jackson library, covering advanced scenarios to equip you with the tools to tackle complex JSON data in Java.
🌐
HowToDoInJava
howtodoinjava.com › home › gson › gson – parse json array to java array or list
Gson - Parse JSON Array to Java Array or List
April 4, 2023 - Here ArrayItem is the class type of data elements in the array. ArrayItem[] userArray = new Gson().fromJson(jsonSource, ArrayItem[].class); For converting such JSON array to a List, we need to use TypeToken class.
🌐
Oracle
docs.oracle.com › javame › 8.0 › api › json › api › index.html
JsonArray (JSON Documentation)
JavaScript is disabled on your browser · Frame Alert · This document is designed to be viewed using the frames feature. If you see this message, you are using a non-frame-capable web client. Link to Non-frame version