我正在尝试从维基百科解析信息。但我有一个问题。我没有从 try catch 中得到错误,但我想要的字符串是 null 。 我实际上得到了整个 Json,但是通过将它们划分为 JSON 会出现问题:
{
"batchcomplete": "",
"query": {
"pages": {
"280636": {
"pageid": 280636,
"ns": 0,
"title": "Gary Moore",
"extract": "Robert William Gary Moore (4 April 1952 – 6 February 2011) was a Northern Irish musician, most widely recognised as a singer and virtuoso guitarist.\nIn a career dating back to the 1960s, Moore played with musicians including Phil Lynott and Brian Downey during his teens, leading him to memberships with the Irish bands Skid Row and Thin Lizzy, and British Band Colosseum II. Moore shared the stage with such blues and rock musicians as B.B. King, Albert King, Jack Bruce, Albert Collins, George Harrison, and Greg Lake, as well as having a successful solo career. He guested on a number of albums recorded by high-profile musicians."
}
}
}
}
我的代码来获取“提取”信息:
try {
JSONObject object = new JSONObject(result); // result is the String with the whole Json
String desc = object.getString("extract"); //this is going to be null
} catch (JSONException e) {
Log.e("JSONException", "Error: " + e.toString());
}
我是 JSON 格式的新手,请帮忙! 这些是双 JSON 对象吗?
最佳答案
试试这个:
JSONObject object = new JSONObject(result);
JSONObject obj1 = object.getJSONObject("query");
JSONObject obj2 = obj1.getJSONObject("pages");
JSONObject obj3=null;
Iterator<String> keys= obj2.keys();
while (keys.hasNext())
{
String keyValue = (String)keys.next();
obj3 = obj2.getJSONObject(keyValue);
}
String desc = obj3.getString("extract");
关于java - 奇怪的 JSON 对象解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31797747/