1

我一直在与我的应用程序中的一些 JSON 解析作斗争,经过 3 天的研究,我仍然无法弄清楚这个问题。

正在发送的错误是“org.json.JSONException: Value”

我一直在处理的 try/catch 语句中出现错误。

我的 Try/Catch 看起来像这样:

Try {
    // Result comes in from an HTTP Request

    JSONArray jarray = new JSONArray(result);
    JSONObject jObj = jarray.getJSONObject(0).getJSONObject("results");
    String TeamName = jObj.getString("fulltext");
} catch (JSONException e) {
    Log.e("JSON Parser", "Error parsing data " + e.toString());
}

我得到的完整 JSON 位于此处,但精简版如下:

{
    "query-continue-offset": 50,
    "query": {
        "printrequests": [{
            "label": "",
            "typeid": "_wpg",
            "mode": 2,
            "format": false
        }],
        "results": {
            "Team:\"Die Unglaublichen\"": {
                "printouts": [],
                "fulltext": "Team:\"Die Unglaublichen\"",
                "fullurl": "http:\/\/wiki.planetkubb.com\/wiki\/Team:%22Die_Unglaublichen%22",
                "namespace": 822,
                "exists": true
            },
            "Team:(Can't Stand) Le Kubb Bricks": {
                "printouts": [],
                "fulltext": "Team:(Can't Stand) Le Kubb Bricks",
                "fullurl": "http:\/\/wiki.planetkubb.com\/wiki\/Team:(Can%27t_Stand)_Le_Kubb_Bricks",
                "namespace": 822,
                "exists": true
            },
            "Team:(OHC) Kubb Team": {
                "printouts": [],
                "fulltext": "Team:(OHC) Kubb Team",
                "fullurl": "http:\/\/wiki.planetkubb.com\/wiki\/Team:(OHC)_Kubb_Team",
                "namespace": 822,
                "exists": true
            },
            "Team:Andrewsons3": {
                "printouts": [],
                "fulltext": "Team:Andrewsons3",
                "fullurl": "http:\/\/wiki.planetkubb.com\/wiki\/Team:Andrewsons3",
                "namespace": 822,
                "exists": true
            }
        },
        "meta": {
            "hash": "46923025c2d5aac3ee963419db93485d",
            "count": 50,
            "offset": 0
        }
    }
}

这是我第一次看到 JSON 代码,老实说,一开始有点困惑,但我可以理解 JSON 是如何工作的,只是不知道如何从这些数组中获取数据!

我错过了一些东西,我只是看不到什么......

4

1 回答 1

0

您得到的错误来自这一行:

String TeamName = jObj.getString("fulltext");

这是因为该值jObj没有“全文”值。相反,该字段是每个 child 的值JSONObject。为避免此错误,您首先需要检查是否JSONObject有变量“全文”:

String TeamName = null;
if (jObj.has("fulltext"))
    TeamName = jObj.getString("fulltext");

为了简化 JSON 解析,droidQuery提供了一些简单的方法来从s 和s 创建Maps 和数组:JSONObjectJSONArray

Map<String, ?> map = $.map(jObj);
for (Entry<String, ?> entry : map.entrySet()) {
    String teamName = entry.key();
    JSONObject json = (JSONObject) entry.value();
    Map<String, ?> team = $.map(json);
    String fulltext = (String) team.get("fulltext");//etc
}
于 2013-07-19T14:28:52.997 回答