Android JSONObject - How can I loop through a flat JSON object to get each key and value

android, java, json

Solution

Use the `keys()` iterator to iterate over all the properties, and call `get()` for each.

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

Problem

``` { "key1": "value1", "key2": "value2", "key3": "value3" } ``` How I can get each item's key and value without knowing the key nor value beforehand?

Original source

Related problems