Chage your code like,

        JSONArray c = jsonObject.getJSONArray("Channel_12");  //Change your code from here
        JSONArray array = c.getJSONArray(0);
        String data = c.getString(1);
        Log.i("#Values", array.getString(0) + "\t" + data);

See my result,

Answer from Jaydipsinh Zala on Stack Overflow
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 19934020 โ€บ looping-nested-through-a-nested-json-object-and-saving-values-in-an-array-list
java - Looping nested through a nested JSON Object and saving values in an array list - Stack Overflow
try{ JSONArray mot = json.getJSONArray("PAYLOAD"); for(int i=0;i<mot.length();i++){ HashMap<String, String> map = new HashMap<String, String>(); JSONObject e = mot.getJSONObject(i); map.put(KEY_NAME_ID, e.getString(KEY_NAME_ID)); map.put(KEY_NAME, e.getString(KEY_NAME)); map.put(KEY_LAT, e.getString(KEY_LAT)); map.put(KEY_LON, e.getString(KEY_LON)); map.put(KEY_VOLUME, e.getString(KEY_VOLUME)); map.put(KEY_VALUE, e.getString(KEY_VALUE)); map.put(KEY_ACCOUNT_STATUS, e.getString(KEY_ACCOUNT_STATUS)); map.put(KEY_CONTRACTOR, e.getString(KEY_CONTRACTOR)); map.put(KEY_SUB_CONTRACTOR, e.getString(KE
Discussions

Parse nested object Json file in JAVA
Hello, I am trying to parse JSON file through JAVA program that has nested objects and arrays. I am attaching the sample JSON file and the expected results. I am also attaching the JAVA class program that I am currently using but it only prints the first object and the values related to it. I want the JAVA code to loop ... More on experts-exchange.com
๐ŸŒ experts-exchange.com
September 11, 2017
java - Building a Nested JSON within looping - Stack Overflow
You need to create new JSON object for each Value,Name, Cities pair. So you need to put it inside second for loop. More on stackoverflow.com
๐ŸŒ stackoverflow.com
java - read nested json object using loop - Stack Overflow
I have been searching from a long time and no solutions is working for me. I have to retrieve the values from the json object using some loop , and number of nested values is random these can be 1 ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Looping through nested json object
I have a badly designed JSON object which unfortunately I cannot change at this moment which contains a number of objects. Here is an example of what I am working with: var land = [ {"name":"c... More on stackoverflow.com
๐ŸŒ stackoverflow.com
August 26, 2016
๐ŸŒ
Experts Exchange
experts-exchange.com โ€บ questions โ€บ 29055896 โ€บ Parse-nested-object-Json-file-in-JAVA.html
Solved: Parse nested object Json file in JAVA | Experts Exchange
September 11, 2017 - import java.io.FileReader; import java.util.Iterator; import java.util.Set; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; /** * @author Crunchify.com */ public class CrunchifyJSONReadFromFile { @SuppressWarnings({ "rawtypes" }) public static void main(String[] args) { JSONParser parser = new JSONParser(); try { Object obj = parser.parse(new FileReader( "/Users/username/Documents/Work_Items.json")); JSONObject jsonObject = (JSONObject) obj; Set keys = jsonObject.keySet(); Iterator a = keys.iterator(); while(a.hasNext()) { String key = (String)a.next(); // loop to get the dynamic key String value = (String)jsonObject.get(key).toString(); System.out.println("key : "+key); System.out.println(" value :"+value); } } catch (Exception e) { e.printStackTrace(); } } }
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 31904690 โ€บ read-nested-json-object-using-loop
java - read nested json object using loop - Stack Overflow
You are trying to use an array as a map. jsonChildObject is actually an JSONArray. It does not have keys; for instance you can have the same {"name":"ipek","key":"1221"} multiple times.
๐ŸŒ
GitHub
github.com โ€บ dabeng โ€บ JSON-Loop
GitHub - dabeng/JSON-Loop: JSON Loop is a super easy to use tool class helping you loop the deeply nested JSON object.
// the first param is the name of 'Id' property of JSON object and the second one // is 'children' property name var jsonloop = new JSONLoop(obj, 'id', 'member');
Starred by 19 users
Forked by 6 users
Languages ย  JavaScript 56.8% | HTML 43.2% | JavaScript 56.8% | HTML 43.2%
Find elsewhere
๐ŸŒ
Reddit
reddit.com โ€บ r/learnjavascript โ€บ iterating through nested json
r/learnjavascript on Reddit: Iterating through Nested JSON
June 1, 2018 -

Hello, not quite sure if this is the best place to ask. Sorry if it isn't.

So the question: I am trying to put all values in an array from some GeoJSON that I have for a popup marker on a map. I am also using React so this may have caused some issues. From what I've been able to gather is the best way to do this is recursively this is my idea of how to accomplish this.

onEachFeature = (feature, layer) => {
    const placesTabContent = [];

    for (const prop in feature.properties.place) {
        if (typeof(feature.properties.place[prop]) === 'object') {
            //Recursive call here?? Not sure how to accomplish.
        } else {
            placesTabContent.push('<b>' + prop + '</b> : ' + feature.properties.place[prop] + '<br>')
        }
      }

    const content = '<div class="tabs">' +

        '<div class="tab" id="places_tab">' +
        '<div class="content">'
        + placesTabContent +
        '</div>' +
        '</div>' +

        '<div class="tab" id="location_tab">' +
        '<div class="content">' +
        + //Will be location info when nested GeoJSON works +
        '</div>' +
        '</div>' +

        '<ul class="tabs-link">' +
        '<li class="tab-link"> <a href="#places_tab"><span>Places</span></a></li>' +
        '<li class="tab-link"> <a href="#location_tab"><span>Location</span></a></li>' +
        '</ul>' +
        '</div>'


    layer.bindPopup(content)
}

onEachFeature gets passed into a GeoJSON component. If this makes no sense let me know. Using Leaflet.js / React-Leaflet.

๐ŸŒ
Webkul
webkul.com โ€บ home โ€บ how to iterate through jsonobject in java and kotlin
How to Iterate through JSONObject in Java and Kotlin - Webkul Blog
January 16, 2026 - Java requires explicit type checking when dealing with nested JSONArrays. This approach ensures proper handling of complex JSON structures with mixed data types. String jsonString = "{\"name\":\"John\", \"age\":30, \"hobbies\":[\"reading\", \"gaming\", \"coding\"]}"; JSONObject mainObject = ...
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 67614724 โ€บ how-to-loop-through-the-nested-data-in-json-while-processing-with-jackson-and-ma
java - How to loop through the nested data in JSON while Processing with Jackson and Map - Stack Overflow
May 20, 2021 - I need a solution that doesn't use a key. instead loops through the data. and fetch key and value 2021-05-20T07:25:47.26Z+00:00 ... try { Object obj = new JSONParser().parse(newFileReader("filename.json")); JSONObject jo = (JSONObject) obj; Map data = ((Map) jo.get("data")); Iterator<Map.Entry> itr1 = data.entrySet().iterator(); while (itr1.hasNext()) { Map.Entry pair = itr1.next(); System.out.println(pair.getKey() + " : " + pair.getValue()); // List<Object> val = (List<Object>) pair.getValue(); JSONObject objs = (JSONObject) pair.getValue(); objs.keySet().forEach(keyStr -> { Object keyvalue = objs.get(keyStr); System.out.println("key: " + keyStr + " value: " + keyvalue); }); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (org.json.simple.parser.ParseException e) { e.printStackTrace(); }
๐ŸŒ
CodingTechRoom
codingtechroom.com โ€บ question โ€บ -java-recursive-loop-jsondata
How to Recursively Loop Through JSON Data Using JSONObject in Java - CodingTechRoom
JSONObject jsonObject = new ... from the org.json library to easily parse and manipulate JSON data. Recursively looping through JSON data allows you to access nested objects and arrays effectively....
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 66408153 โ€บ how-to-loop-trough-nested-json-and-get-value-using-java-springboot
How to loop trough nested JSON and get value using Java, SpringBoot - Stack Overflow
February 28, 2021 - I just updated my question I have a problem with nested for loop and part that you have updated. In the first loop I try to iterate through CarResponse list to get Car because it contains CarValues that contain field name and price. But I guess I am doing something wrong because cars.getCarValue() in second loop is underlined red with error foreach not applicable to type 'modal.CarValue' 2021-02-28T16:13:41.063Z+00:00 ... According to Car.java class, the property with the name carValue has a type of List<CarValue>, but the error you are getting means that the method cars.getCarValue() returns a single object of type CarValue, and you can't loop over it, obviously, since it is not a List, hence the error.
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ json โ€บ iterating over an instance of org.json.jsonobject
Iterating Over an Instance of org.json.JSONObject | Baeldung
May 5, 2025 - Letโ€™s start with the simple case of iterating a JSON of name-value pairs: { "name": "Cake", "cakeId": "0001", "cakeShape": "Heart" } For this, we can simply iterate through the keys using the keys() method: void handleJSONObject(JSONObject jsonObject) { jsonObject.keys().forEachRemaining(key ...
Top answer
1 of 7
36

Maybe you're not using the latest version of a JSON for Java Library.

json-simple has not been updated for a long time, while JSON-Java was updated 2 month ago.

JSON-Java can be found on GitHub, here is the link to its repo: https://github.com/douglascrockford/JSON-java

After switching the library, you can refer to my sample code down below:

public static void main(String[] args) {
    String JSON = "{\"LanguageLevels\":{\"1\":\"Pocz\\u0105tkuj\\u0105cy\",\"2\":\"\\u015arednioZaawansowany\",\"3\":\"Zaawansowany\",\"4\":\"Ekspert\"}}\n";

    JSONObject jsonObject = new JSONObject(JSON);
    JSONObject getSth = jsonObject.getJSONObject("LanguageLevels");
    Object level = getSth.get("2");

    System.out.println(level);
}

And as JSON-Java open-sourced, you can read the code and its document, they will guide you through.

Hope that it helps.

2 of 7
15

You will have to iterate step by step into nested JSON.

for e.g a JSON received from Google geocoding api

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "Bhopal",
               "short_name" : "Bhopal",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Bhopal",
               "short_name" : "Bhopal",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Madhya Pradesh",
               "short_name" : "MP",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "India",
               "short_name" : "IN",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Bhopal, Madhya Pradesh, India",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 23.3326697,
                  "lng" : 77.5748062
               },
               "southwest" : {
                  "lat" : 23.0661497,
                  "lng" : 77.2369767
               }
            },
            "location" : {
               "lat" : 23.2599333,
               "lng" : 77.412615
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 23.3326697,
                  "lng" : 77.5748062
               },
               "southwest" : {
                  "lat" : 23.0661497,
                  "lng" : 77.2369767
               }
            }
         },
         "place_id" : "ChIJvY_Wj49CfDkR-NRy1RZXFQI",
         "types" : [ "locality", "political" ]
      }
   ],
   "status" : "OK"
}

I shall iterate in below given fashion to "location" : { "lat" : 23.2599333, "lng" : 77.412615

//recieve JSON in json object

        JSONObject json = new JSONObject(output.toString());
        JSONArray result = json.getJSONArray("results");
        JSONObject result1 = result.getJSONObject(0);
        JSONObject geometry = result1.getJSONObject("geometry");
        JSONObject locat = geometry.getJSONObject("location");

        //"iterate onto level of location";

        double lat = locat.getDouble("lat");
        double lng = locat.getDouble("lng");