Android JSONObject - 如何遍历平面JSON对象以获取每个键和值

{

  "key1": "value1",

  "key2": "value2",

  "key3": "value3"

}

如何在不事先知道钥匙或价值的情况下获得每件物品的钥匙和价值?


慕码人8056858
浏览 3221回答 3
3回答

梵蒂冈之花

使用keys()迭代器迭代所有属性,并调用get()每个属性。Iterator<String> iter = json.keys();while (iter.hasNext()) {&nbsp; &nbsp; String key = iter.next();&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; Object value = json.get(key);&nbsp; &nbsp; } catch (JSONException e) {&nbsp; &nbsp; &nbsp; &nbsp; // Something went wrong!&nbsp; &nbsp; }}

慕无忌1623718

for(Iterator<String> iter = json.keys();iter.hasNext();) {&nbsp; &nbsp; String key = iter.next();&nbsp; &nbsp; ...}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java
Android