开发者社区> 问答> 正文

在Android中解析JSON显示不匹配错误

我正在尝试从牛津词典解析数据。但是,我得到的价值找不到错误。

{
    "id": "ace",
    "metadata": {
        "operation": "retrieve",
        "provider": "Oxford University Press",
        "schema": "RetrieveEntry"
    },
    "results": [
        {
            "id": "ace",
            "language": "en-gb",
            "lexicalEntries": [
                {
                    "entries": [
                        {
                            "homographNumber": "100",
                            "senses": [
                                {
                                    "definitions": [
                                        "a playing card with a single spot on it, ranked as the highest card in its suit in most card games"
                                    ],
                                    "id": "m_en_gbus0005680.006"
                                },
                                {
                                    "definitions": [
                                        "a person who excels at a particular sport or other activity"
                                    ],
                                    "id": "m_en_gbus0005680.010",
                                    "subsenses": [
                                        {
                                            "definitions": [
                                                "a pilot who has shot down many enemy aircraft"
                                            ],
                                            "id": "m_en_gbus0005680.011"
                                        }
                                    ]
                                },
                                {
                                    "definitions": [
                                        "(in tennis and similar games) a service that an opponent is unable to return and thus wins a point"
                                    ],
                                    "id": "m_en_gbus0005680.013",
                                    "subsenses": [
                                        {
                                            "definitions": [
                                                "a hole in one"
                                            ],
                                            "id": "m_en_gbus0005680.014"
                                        }
                                    ]
                                }
                            ]
                        }
                    ],
                    "language": "en-gb",
                    "lexicalCategory": {
                        "id": "noun",
                        "text": "Noun"
                    },
                    "text": "ace"
                },
                {
                    "entries": [
                        {
                            "homographNumber": "101",
                            "senses": [
                                {
                                    "definitions": [
                                        "very good"
                                    ],
                                    "id": "m_en_gbus0005680.016"
                                }
                            ]
                        }
                    ],
                    "language": "en-gb",
                    "lexicalCategory": {
                        "id": "adjective",
                        "text": "Adjective"
                    },
                    "text": "ace"
                },
                {
                    "entries": [
                        {
                            "homographNumber": "102",
                            "senses": [
                                {
                                    "definitions": [
                                        "(in tennis and similar games) serve an ace against (an opponent)"
                                    ],
                                    "id": "m_en_gbus0005680.020",
                                    "subsenses": [
                                        {
                                            "definitions": [
                                                "score an ace on (a hole) or with (a shot)"
                                            ],
                                            "id": "m_en_gbus0005680.026"
                                        }
                                    ]
                                },
                                {
                                    "definitions": [
                                        "achieve high marks in (a test or exam)"
                                    ],
                                    "id": "m_en_gbus0005680.028",
                                    "subsenses": [
                                        {
                                            "definitions": [
                                                "outdo someone in a competitive situation"
                                            ],
                                            "id": "m_en_gbus0005680.029"
                                        }
                                    ]
                                }
                            ]
                        }
                    ],
                    "language": "en-gb",
                    "lexicalCategory": {
                        "id": "verb",
                        "text": "Verb"
                    },
                    "text": "ace"
                }
            ],
            "type": "headword",
            "word": "ace"
        },
        {
            "id": "ace",
            "language": "en-gb",
            "lexicalEntries": [
                {
                    "entries": [
                        {
                            "homographNumber": "200",
                            "senses": [
                                {
                                    "definitions": [
                                        "a person who has no sexual feelings or desires"
                                    ],
                                    "id": "m_en_gbus1190638.004"
                                }
                            ]
                        }
                    ],
                    "language": "en-gb",
                    "lexicalCategory": {
                        "id": "noun",
                        "text": "Noun"
                    },
                    "text": "ace"
                },
                {
                    "entries": [
                        {
                            "homographNumber": "201",
                            "senses": [
                                {
                                    "definitions": [
                                        "(of a person) having no sexual feelings or desires; asexual"
                                    ],
                                    "id": "m_en_gbus1190638.006"
                                }
                            ]
                        }
                    ],
                    "language": "en-gb",
                    "lexicalCategory": {
                        "id": "adjective",
                        "text": "Adjective"
                    },
                    "text": "ace"
                }
            ],
            "type": "headword",
            "word": "ace"
        }
    ],
    "word": "ace"
}

这是我用来解析数据的Java代码。该代码尚未完成。但是,我只是想测试开始的解码阶段。

//data is string
 OfflineWordItems o = new OfflineWordItems();
        try{
            JSONObject o0 = new JSONObject(data);
            JSONArray a1 = o0.getJSONArray("results");
            for (int i = 0; i<a1.length(); i++){
                JSONObject o2 = a1.getJSONObject(i);
                JSONArray a2 = o2.getJSONArray("lexicalEntries");
                for(int i2 = 0; i2<a2.length(); i2++){
                    JSONObject o3 = a2.getJSONObject(i2);
                    JSONArray a3 = o3.getJSONArray("entries");
                    for(int i3 = 0; i3<a3.length(); i3++){
                        JSONObject o4 = a3.getJSONObject(i3);
                        JSONArray a4 = o4.getJSONArray("senses");
                        for(int i4=0; i4<a4.length(); i4++){
                            JSONObject o5 = a4.getJSONObject(i4);
                            o.definition = o5.getString("definitions");
                        }
                    }
                    JSONObject o6 = o3.getJSONObject("lexicalCategory");
                    o.lexicalCategoryId = o6.getString("id");
                    o.lexicalCategoryText = o6.getString("text");
                }
            }
            return o;
        } catch (Exception e){
            e.printStackTrace();
            return null;
        }

这是我得到的错误。我尝试了其他解码模式。然后显示不匹配类型错误。

W/System.err: org.json.JSONException: No value for results
W/System.err:     at org.json.JSONObject.get(JSONObject.java:392)
W/System.err:     at org.json.JSONObject.getJSONArray(JSONObject.java:587)

编辑:即使我只写这些行:

JSONObject o0 = new JSONObject(data);
            JSONArray a1 = o0.getJSONArray("results");

我仍然遇到相同的错误。

展开
收起
几许相思几点泪 2019-12-05 15:36:22 571 0
1 条回答
写回答
取消 提交回答
  • 为何不用fastjson

    2019-12-05 16:28:24
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
58同城Android客户端Walle框架演进与实践之路 立即下载
Android组件化实现 立即下载
蚂蚁聚宝Android秒级编译——Freeline 立即下载

相关镜像