2017-05-05 3 views
2

JSON을 매우 처음 사용했습니다. json-lib를 사용하여 옥스포드 사전 API에 의해이 JSON 응답에서 "정의"를 얻으려고 노력했습니다. 모든 종류의 것을 시도했다. 그러나 "결과"의 모든 것보다 더 구체적인 것을 얻을 수는 없습니다. 이것이 왜 그렇게 어려운지 알아 내고 싶습니다. 그렇지 않으면 정규 표현식에 의지 할 것입니다.사전 API에서 Java로 JSON 구문 분석

 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

'String test = 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(); 

참조 : 우리는 JSON 객체를 통과하고 http://jsonviewer.stack.hu/
시계 조심스럽게 길을 우리는 특정를 검색 할 getJSONObject & getJSONArray 방법을 사용해야 할 때 JSON Object에서 원하는 데이터 유형.

enter image description here

업데이트 : 당신은 당신의 필요 조건에 따라, JSON 배열을 반복 할 수 있습니다. 방금 definitions까지 연락하는 방법을 설명했습니다.

+0

굉장합니다. 대단히 감사합니다. 당신은 전설입니다. –

+1

업데이트 된 코드를 확인하십시오. 이해하는 것이 훨씬 쉽습니다. –

+1

나는 전설이 아니다. : SO에 대해 높은 평점을받은 사용자가 많다;) 나는 그저 비행 중에 배우고 그것을 해결하기 전에 나 자신을 시험해 본다. –