userimage

Iterate through JSONObject from root in json simple

Want to Iterate below JSon object in java

{
"k1":"v1",
"k2":"v2",
"k3":"v3"
}

by using Iterator iter = jObj.keys();

And got the below exception,

throws: cannot find symbol symbol : method keys() location: class org.json.simple.JSONObject

userimage

Hope it will be solve your problem,


JSONParser parser = new JSONParser();

Object obj = parser.parse(new FileReader("simple.json")); //Assuming your JSON object is saved in a file "simple.json"

JSONObject jsonObject = (JSONObject) obj;

for(Iterator iterator = jsonObject.keySet().iterator(); iterator.hasNext();) {
    String key = (String) iterator.next();
    System.out.println(jsonObject.get(key));
}


Answer is