Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

how to iterate over JSONObject

import org.json.JSONObject;

public static void printJsonObject(JSONObject jsonObj) {
    for (String keyStr : jsonObj.keySet()) {
        Object keyvalue = jsonObj.get(keyStr);

        //Print key and value
        System.out.println("key: "+ keyStr + " value: " + keyvalue);

        //for nested objects iteration if required
        //if (keyvalue instanceof JSONObject)
        //    printJsonObject((JSONObject)keyvalue);
    }
}
Comment

how to iterate over JSONObject

import org.json.JSONObject;

public static void printJsonObject(JSONObject jsonObj) {
    jsonObj.keySet().forEach(keyStr ->
    {
        Object keyvalue = jsonObj.get(keyStr);
        System.out.println("key: "+ keyStr + " value: " + keyvalue);

        //for nested objects iteration if required
        //if (keyvalue instanceof JSONObject)
        //    printJsonObject((JSONObject)keyvalue);
    });
}
Comment

how to iterate over JSONObject

JSONObject jsonObject = new JSONObject(contents.trim());
Iterator<String> keys = jsonObject.keys();

while(keys.hasNext()) {
    String key = keys.next();
    if (jsonObject.get(key) instanceof JSONObject) {
          // do something with jsonObject here      
    }
}
Comment

PREVIOUS NEXT
Code Example
Java :: android vibrate 
Java :: java first day of week 
Java :: java infinitew recursion 
Java :: Exception in thread "main" java.lang.NoClassDefFoundError: javax/xml/bind/annotation/XmlSchema 
Java :: import random java 
Java :: setbounds in java 
Java :: check if all values are same in list java 
Java :: write json file java 
Java :: java char to charsequence 
Java :: java int to string 
Java :: java_home should point to a jdk not a jre 
Java :: read integer input java 
Java :: How to efficiently find three largest numbers of array, in Java? 
Java :: android recyclerview show hide item with animation with default animation 
Java :: what it means when create final variable in java 
Java :: java insert into arraylist 
Java :: how to get width android 
Java :: java change text on click 
Java :: how to compare current date and time with another date and time in android 
Java :: java split string 
Java :: recycler view implementation 
Java :: AmazonS3ClientBuilder 
Java :: Android dark theme programmatically 
Java :: set password visible in android 
Java :: why bufferedreader is faster than scanner 
Java :: unprocessed continuation reference(s) remaining name 
Java :: java thread class sleep 
Java :: max of an array java 
Java :: generate random password in spring boot 
Java :: printing 2d array in java 
ADD CONTENT
Topic
Content
Source link
Name
2+3 =