这里我从本地资产文件夹中获取了JSON。所有JSON对象的名称都不同。 1号线,3号线,5号线,7号线......一直到1000号。我附上了json供你参考。
{
"status": {
"rcode": 200,
"message": "OK"
},
"data": {
"0": {
"OutletName": "Test 1 ",
"Latitude": "16.123234",
"Longitude": "79.546745"
},
"1": {
"OutletName": "Test 2",
"Latitude": "16.343234",
"Longitude": "79.786745"
},
"2": {
"OutletName": "Test 3",
"Latitude": "19.1294",
"Longitude": "72.836122"
},
"3": {
"OutletName": "Test 4",
"Latitude": "19.136383",
"Longitude": "72.827997"
},
"6": {
"OutletName": "Test 5",
"Latitude": "19.136715",
"Longitude": "72.829248"
},
"7": {
"OutletName": "Test 6",
"Latitude": "19.128483",
"Longitude": "72.821199"
},
"8": {
"OutletName": "Test 7",
"Latitude": "19.128528",
"Longitude": "72.819388"
},
"10": {
"OutletName": "Test 8",
"Latitude": "19.140333",
"Longitude": "72.831095"
},
"11": {
"OutletName": "Test 9",
"Latitude": "19.14027",
"Longitude": "72.826285"
}
}
}
这里对象名称对所有人都不同。所以我试试这个
private void parseJson() {
try {
JSONObject obj = new JSONObject(loadJSONFromAsset());
JSONObject jObj = obj.getJSONObject("data");
System.out.println("jObj.length()==> " + jObj.length());
for (int i = 0; i < jObj.length(); i++) {
if (jObj.has(String.valueOf(i)) && !jObj.isNull(String.valueOf(i))) {
JSONObject jObj1 = jObj.getJSONObject(String.valueOf(i));
System.out.println("Index==> "+i);
System.out.println("OutletName==> "+jObj1.getString("OutletName"));
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
但在这里我没有获得所有数据。请帮我解析一下这种JSON。
提前致谢。
答案 0 :(得分:1)
您需要使用keys
方法,如下所示:
private void parseJson() {
try {
JSONObject obj = new JSONObject(loadJSONFromAsset());
JSONObject jObj = obj.getJSONObject("data");
Iterator iter = jObj.keys()
while(iter.hasNext()) {
String key = (String) iter.next();
JSONObject jObj1 = jObj.getJSONObject(key);
System.out.println("Index==> "+ key);
System.out.println("OutletName==> "+jObj1.getString("OutletName"));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
修改强> 修复了一些类型转换
答案 1 :(得分:0)
&#34;数据&#34;的价值在你的JSON中不是一个数组,因此你不能直接迭代它。您需要先将其转换为HashMap,然后迭代键集。您可以使用Jackson library将JSONObject转换为HashMap。您也可以参考this answer转换为地图。
答案 2 :(得分:0)
首先,您必须对json响应进行更改
{
"status": {
"rcode": 200,
"message": "OK"
},
"data": { //this has to be array in your json
"0": { // and these are object inside the arrays
"OutletName": "Test 1 ",
"Latitude": "16.123234",
"Longitude": "79.546745"
},
"1": { // and these are object inside the arrays
"OutletName": "Test 2",
"Latitude": "16.343234",
"Longitude": "79.786745"
},
"2": {
"OutletName": "Test 3",
"Latitude": "19.1294",
"Longitude": "72.836122"
},
"3": {
"OutletName": "Test 4",
"Latitude": "19.136383",
"Longitude": "72.827997"
},
"6": {
"OutletName": "Test 5",
"Latitude": "19.136715",
"Longitude": "72.829248"
},
"7": {
"OutletName": "Test 6",
"Latitude": "19.128483",
"Longitude": "72.821199"
},
"8": {
"OutletName": "Test 7",
"Latitude": "19.128528",
"Longitude": "72.819388"
},
"10": {
"OutletName": "Test 8",
"Latitude": "19.140333",
"Longitude": "72.831095"
},
"11": {
"OutletName": "Test 9",
"Latitude": "19.14027",
"Longitude": "72.826285"
}
}
}
并且不要超过json对象的大小来访问它的对象它必须是JSONArray 像这样
获取JSONArray中的数组数据JSONArray dataArray = response_obj.getJSONArray("data");
然后像这样迭代
for (int i = 0; i < dataArray.length(); i++)
{
//write your code here to access all the object as for loop executes on by one
}