Iterate through JSONObject from root in json simple

后端 未结 3 1770
梦谈多话
梦谈多话 2020-12-13 04:52

I am trying to iterate over a json object using json simple. I have seen answers where you can do a getJSONObject(\"child\") from

{ \"child\":          


        
3条回答
  •  青春惊慌失措
    2020-12-13 05:09

    Assuming your JSON object is saved in a file "simple.json", you can iterate over the attribute-value pairs as follows:

    JSONParser parser = new JSONParser();
    
    Object obj = parser.parse(new FileReader("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));
    }
    

提交回复
热议问题