K先生给出的答案也是对的,但您也可以使用jsonObject names()方法.请找到示例代码
for(int i = 0; i
上面的示例json数组,如何获得204,203和202?
不,当前的String JSONObject
代替JSONArray
.你应该使用JSONObject获取Iterator .keys()如果内部JSONObject键动态为:
JSONObject issueObj = new JSONObject(jsonContent); Iterator iterator = issueObj.keys(); while(iterator.hasNext()){ String key = (String)iterator.next(); JSONObject issue = issueObj.getJSONObject(key); // get id from issue String _pubKey = issue.optString("id"); }
K先生给出的答案也是对的,但您也可以使用jsonObject names()方法.请找到示例代码
for(int i = 0; i<jsonobject.length(); i++){ Log.e(TAG, "Key = " + jsonobject.names().getString(i) + " value = " + jsonobject.get(jsonobject.names().getString(i))); }
我希望dis会帮助你
使用此方法动态迭代json
private void parseJson(JSONObject data) { if (data != null) { Iterator<String> it = data.keys(); while (it.hasNext()) { String key = it.next(); try { if (data.get(key) instanceof JSONArray) { JSONArray arry = data.getJSONArray(key); int size = arry.length(); for (int i = 0; i < size; i++) { parseJson(arry.getJSONObject(i)); } } else if (data.get(key) instanceof JSONObject) { parseJson(data.getJSONObject(key)); } else { System.out.println("" + key + " : " + data.optString(key)); } } catch (Throwable e) { System.out.println("" + key + " : " + data.optString(key)); e.printStackTrace(); } } } }