首页 > 解决方案 > 在android中解析JSON显示不匹配错误

问题描述

我正在尝试解析牛津词典中的数据。但是,我收到 value not found 错误。

{
  "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");

我仍然遇到同样的错误。

标签: javaandroidjson

解决方案


this work for JSONObject:


   JSONObject jsonObject=new JSONObject(data);

                JSONArray jsonArray=jsonObject.getJSONArray("results");

                for(int i=0;i<jsonArray.length();i++){

               JSONArray  
          jsonArray1=jsonArray.getJSONObject(i).getJSONArray("lexicalEntries");

                    for(int j=0;j<jsonArray1.length();j++){

                        JSONArray 
            jsonArray2=jsonArray1.getJSONObject(j).getJSONArray("entries");

                        for(int k=0;k<jsonArray2.length();k++){

                            JSONArray 
                 jsonArray3=jsonArray2.getJSONObject(k).getJSONArray("senses");

                            for(int l=0;l<jsonArray3.length();l++){

                                JSONArray 
               jsonArray4=jsonArray3.getJSONObject(l).getJSONArray("definitions");

                                for(int m=0;m<jsonArray4.length();m++){

                                    JSONObject 
                 definitionsJsonObject=jsonArray4.getJSONObject(m);

                                }

                            }

                        }

                    }
                }

推荐阅读