How to iterate over a JSONObject?

后端 未结 16 1876
孤街浪徒
孤街浪徒 2020-11-22 04:17

I use a JSON library called JSONObject (I don\'t mind switching if I need to).

I know how to iterate over JSONArrays, but when I parse JSO

16条回答
  •  时光说笑
    2020-11-22 04:33

    Maybe this will help:

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

提交回复
热议问题