【问题标题】:How to Parse a JSON Object In Android如何在 Android 中解析 JSON 对象
【发布时间】:2011-07-30 19:33:40
【问题描述】:

我在从 JSON 对象中提取值时遇到一些问题。这是我的代码

try {
    JSONObject json = new JSONObject(result);
    JSONObject json2 = json.getJSONObject("results");
    test = json2.getString("name");     
} catch (JSONException e) {
    e.printStackTrace();
}

test 被声明为String。代码运行时显示null。如果我在调试模式下将鼠标悬停在 json2 上,我可以看到对象中的所有值和名称。

我也试过了

test = json2.length();

这返回了test = 0。即使我将鼠标悬停在json2 对象上,我也可以读取对象中的值。

这是我将使用的 JSON 字符串示例。

{
    "caller":"getPoiById",
    "results":
    {
        "indexForPhone":0,
        "indexForEmail":"NULL",
        "indexForHomePage":"NULL",
        "indexForComment":"NULL",
        "phone":"05137-930 68",
        "cleanPhone":"0513793068",
        "internetAccess":"2",
        "overnightStay":"2",
        "wasteDisposal":"2",
        "toilet":"2",
        "electricity":"2",
        "cran":"2",
        "slipway":"2",
        "camping":"2",
        "freshWater":"2",
        "fieldNamesWithValue":["phone"],
        "fieldNameTranslations": ["Telefon"],
        "id":"1470",
        "name":"Marina Rasche Werft GmbH & Co. KG",
        "latitude":"52.3956107286487",
        "longitude":"9.56583023071289"
    }
}

【问题讨论】:

  • 您提供的 JSON 字符串示例至少对我有用。您是否也遇到了该字符串的问题?
  • 你能显示整个代码吗?或者至少帮我解决我的jsonobject问题

标签: android json


【解决方案1】:

最后我通过使用JSONObject.get 而不是JSONObject.getString 解决了它,然后将test 转换为String

private void saveData(String result) {
    try {
        JSONObject json= (JSONObject) new JSONTokener(result).nextValue();
        JSONObject json2 = json.getJSONObject("results");
        test = (String) json2.get("name");
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

【讨论】:

  • 我遇到了同样的问题,按照你的方法解决了。您是否寻找/设法找到解释为什么这有效但 getJSONObject 无效?
  • 因为你的 json 是一个对象而不是一个数组所以你使用 Jsonobject
【解决方案2】:

在您的 JSON 格式中,它没有起始 JSON 对象

喜欢:

{
    "info" :       <!-- this is starting JSON object -->
        {
        "caller":"getPoiById",
        "results":
        {
            "indexForPhone":0,
            "indexForEmail":"NULL",
            .
            .
         }
    }
}

以上 Json 以 info 作为 JSON 对象开始。所以在执行时:

JSONObject json = new JSONObject(result);    // create JSON obj from string
JSONObject json2 = json.getJSONObject("info");    // this will return correct

现在,我们可以访问result 字段:

JSONObject jsonResult = json2.getJSONObject("results");
test = json2.getString("name"); // returns "Marina Rasche Werft GmbH & Co. KG"

我认为这是缺失的,所以当我们使用 JSONTokener 就像你的回答一样时,问题就解决了。

你的回答很好。只是我想我添加了这些信息,所以我回答了

谢谢

【讨论】:

    【解决方案3】:

    【讨论】:

    • 我在上面发布了更新的代码,但我仍然有同样的问题@Dave G
    【解决方案4】:
    JSONArray jsonArray = new JSONArray(yourJsonString);
    
    for (int i = 0; i < jsonArray.length(); i++) {
         JSONObject obj1 = jsonArray.getJSONObject(i);
         JSONArray results = patient.getJSONArray("results");
         String indexForPhone =  patientProfile.getJSONObject(0).getString("indexForPhone"));
    }
    

    改成 JSONArray,再转换成 JSONObject。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-26
      • 1970-01-01
      • 2016-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-05
      • 1970-01-01
      相关资源
      最近更新 更多