0
我有这样的JSON字符串:解析JSON对象和JSON数组中的Android
{
"query": {
"pages": {
"53113": {
"pageid": 53113,
"ns": 0,
"title": "Charing Cross",
"coordinates": [
{
"lat": 51.5073,
"lon": -0.12755,
"primary": "",
"globe": "earth"
}
]
},
"33109245": {
"pageid": 33109245,
"ns": 0,
"title": "Equestrian statue of Charles I, Charing Cross",
"coordinates": [
{
"lat": 51.5073,
"lon": -0.12768,
"primary": "",
"globe": "earth"
}
]
},
"4347521": {
"pageid": 4347521,
"ns": 0,
"title": "Greater London Built-up Area",
"coordinates": [
{
"lat": 51.5073,
"lon": -0.1277,
"primary": "",
"globe": "earth"
}
]
},
"17867": {
"pageid": 17867,
"ns": 0,
"title": "London",
"coordinates": [
{
"lat": 51.5072,
"lon": -0.1275,
"primary": "",
"globe": "earth"
}
]
}
}
}
}
何我分析它? 我写了这段代码,但我无法遍历json字符串。 我需要“标题”对象和“lat”和“lon”的“坐标”数组...
我最终解决了这个问题。谢谢大家 解决这样的:
try {
// Parsing JSON String or URL
JSONObject jsonObj = new JSONObject(jsonurl);
// grabbing objects
JSONObject obj_query = jsonObj.getJSONObject(TAG_QUERY);
JSONObject obj_pages = obj_query.getJSONObject(TAG_PAGES);
JSONArray arr_id = obj_pages.names();
for (int i = 0 ; i < arr_id.length() ; i ++)
{
JSONObject obj_id = obj_pages.getJSONObject(arr_id.get(i).toString());
// Log.i(LOGTAG, "obj_id: " + obj_id.toString());
String tag_pageid = obj_id.getString(TAG_PAGEID);
// String tag_ns = obj_id.getString(TAG_NS);
String tag_title = obj_id.getString(TAG_TITLE);
Log.i(LOGTAG, "page id: " + tag_pageid);
// Log.i(LOGTAG, tag_ns);
Log.i(LOGTAG, "Title: " + tag_title);
// using JSONArray to grab the TAG_COORDINATES
JSONArray arr_coord = obj_id.getJSONArray(TAG_COORDINATES);
// lets loop through the JSONArray and get all the items
for (int j = 0; j < arr_coord.length(); j++) {
// printing the values to the logcat
Log.i(LOGTAG, "lat:" + arr_coord.getJSONObject(j).getString(TAG_LAT).toString());
Log.i(LOGTAG, "lon: " + arr_coord.getJSONObject(j).getString(TAG_LON).toString());
}
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
我不明白什么是你的问题,我只是运行你的代码和所有工作好... – GhostDerfel
pages.keys()...在这里问了很多次 – Selvin
这个'''JSONObject tag_id = tag_pages.getJSONObject(TAG_PAGES);'''看起来应该改成更像这样的东西''' 'JSONObject tag_id = tag_pages.getJSONObject(TAG_ID);'''我怀疑这行代码没有返回你期望的json。你想把Id从'''tag_pages'''对象中移出吗? – Anderman