How to iterate over a JSONObject?

后端 未结 16 2121
孤街浪徒
孤街浪徒 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:38

    Can't believe that there is no more simple and secured solution than using an iterator in this answers...

    JSONObject names () method returns a JSONArray of the JSONObject keys, so you can simply walk though it in loop:

    JSONObject object = new JSONObject ();
    JSONArray keys = object.names ();
    
    for (int i = 0; i < keys.length (); i++) {
       
       String key = keys.getString (i); // Here's your key
       String value = object.getString (key); // Here's your value
       
    }
    

提交回复
热议问题