java - 奇怪的 JSON 对象解析

标签 java android json

我正在尝试从维基百科解析信息。但我有一个问题。我没有从 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/

相关文章:

java - 从 PostgreSQL 表中选择行范围

android - 解释顶级 gradle 文件的新格式

使用 Amazon EC2 和 SimpleDB 的 Android 应用程序设计

ios - 使用 Decodable 将 JSON 解析为 TableView 部分,数组中包含数组

java - 获取发布到 JAXB Web 服务端点的 XML 字符串

java - 设置 int 的半字节 - java

java - jasperreport生成的条形码无法扫描

java - 如何使用uuid :randomUUID() in policy builder @Novell

java - 无法在android数据绑定(bind)中制作点击事件

ios - 将 SQLite3 数据库转换为 JSON iOS