{ "key1": "value1", "key2": "value2", "key3": "value3" }
如何在不事先知道键或值的情况下获取每个项目的键和值?
使用keys()迭代器迭代所有属性,并get()为每个属性调用。
keys()
get()
Iterator<String> iter = json.keys(); while (iter.hasNext()) { String key = iter.next(); try { Object value = json.get(key); } catch (JSONException e) { // Something went wrong! } }