create an array of all the keys in a JSONObject Android

后端 未结 3 1117
执念已碎
执念已碎 2021-01-19 19:38

Hi im wanting to create an array of all the keys in a JSONObject. my understanding (please correct me if i\'m wrong) is that i need to convert the JSONObject to a Map and th

3条回答
  •  孤街浪徒
    2021-01-19 19:53

    Use the [keys()][1] iterator to iterate over all the properties, and call [get()][2] for each.

    Iterator iter = json.keys();
    while (iter.hasNext()) {
        String key = iter.next();
        try {
            Object value = json.get(key);
        } catch (JSONException e) {
            // Something went wrong!
        }
    }
    

提交回复
热议问题