2017-05-05 115 views
2

JSON非常新。使用json-lib做這件事,並且一直拉着我的頭髮,試圖從牛津字典API的這個JSON響應中獲得「定義」。嘗試了各種各樣的事情。但從來沒有比「結果」中的所有東西都更具體。想弄清楚爲什麼這是如此困難,否則就會訴諸正則表達式。從字典api解析JSON與Java

 JSONObject obj = new JSONObject(stringBuilder.toString()); 

     JSONArray arr = obj.getJSONArray("results"); 
     String test = arr.getJSONObject(1).toString(); // Empty wtf? 
     String definition = obj.getString("definitions"); // empty also... 

管理也進入「ID」,「語言」和「lexicalEntries」分開,但任何超出似乎並不想合作....這是一個正常的JSON響應?這很尷尬不?謝謝...

JSON:

{ 
    "metadata": { 
    "provider": "Oxford University Press" 
    }, 
    "results": [ 
    { 
     "id": "ace", 
     "language": "en", 
     "lexicalEntries": [ 
     { 
      "entries": [ 
      { 
       "homographNumber": "000", 
       "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", 
      "lexicalCategory": "Noun", 
      "text": "ace" 
     }, 
     { 
      "entries": [ 
      { 
       "homographNumber": "001", 
       "senses": [ 
       { 
        "definitions": [ 
        "very good" 
        ], 
        "id": "m_en_gbus0005680.016" 
       } 
       ] 
      } 
      ], 
      "language": "en", 
      "lexicalCategory": "Adjective", 
      "text": "ace" 
     }, 
     { 
      "entries": [ 
      { 
       "homographNumber": "002", 
       "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", 
      "lexicalCategory": "Verb", 
      "text": "ace" 
     } 
     ], 
     "type": "headword", 
     "word": "ace" 
    } 
    ] 
} 
+1

假設'字符串測試= arr.getJSONObject(0).toString();'有值inside.it最好使用像http://jsonviewer.stack.hu/工具來擴展json和分析。 –

+0

感謝您的鏈接,這是一個很好的資源。 –

回答

2
String jsonData = sb.toString(); 
JSONObject obj = new JSONObject(jsonData); 

JSONArray resultsArr = obj.getJSONArray("results"); 
String test = resultsArr.getJSONObject(0).toString(); 

JSONArray lexicalEntriesArr = resultsArr.getJSONObject(0).getJSONArray("lexicalEntries"); 
JSONArray entriesArr = lexicalEntriesArr.getJSONObject(0).getJSONArray("entries"); 
JSONArray sensesArr = entriesArr.getJSONObject(0).getJSONArray("senses"); 
JSONArray definitionsArr = sensesArr.getJSONObject(0).getJSONArray("definitions"); 

String definition = definitionsArr.toString(); 

參考:http://jsonviewer.stack.hu/我們遍歷JSON對象和
觀察仔細的方式時,我們應該使用getJSONObject & getJSONArray方法來檢索特定我們希望從JSON對象中獲取數據的類型。

enter image description here

更新:您可能希望JSON數組遍歷按您的要求。我剛剛解釋過如何達到definitions

+0

太棒了。謝謝你,先生。你是一個傳奇。 –

+1

請檢查更新的代碼。理解起來要簡單得多。 –

+1

我不是一個傳奇:p有很多高度評價的用戶,因此我只是在飛行中學習,在嘗試解決它之前嘗試自己的東西。 –