2017-02-03 2 views
1

저는 JSON에 조금 익숙하며 elasticsearch에서 반환 한 값을 가져 오려고합니다. "* org.json.simple을."라이브러리 내가 사용하려고Java에서 중첩 JSON 구문 분석

{ 
    "took": 17, 
    "timed_out": false, 
    "_shards": { 
    "total": 1, 
    "successful": 1, 
    "failed": 0 
    }, 
    "hits": { 
    "total": 32, 
    "max_score": 2.6414862, 
    "hits": [ 
     { 
     "_index": "pokedex", 
     "_type": "pokemon", 
     "_id": "65", 
     "_score": 2.6414862, 
     "_source": { 
      "ndex": 65, 
      "name": "Alakazam", 
      "date_captured": "2015-11-03", 
      "has_evolution": false, 
      "type": "Psychic", 
      "how_to_find": [ 
      { 
       "game": "Red", 
       "method": "Evolve" 
      }, 
      { 
       "game": "Blue", 
       "method": "Evolve" 
      }, 
      { 
       "game": "Gold", 
       "method": "Evolve" 
      }, 
      { 
       "game": "Silver", 
       "method": "Evolve" 
      } 
      ], 
      "height": 59, 
      "weight": 105.8 
     } 
     }, 
     { 
     "_index": "pokedex", 
     "_type": "pokemon", 
     "_id": "96", 
     "_score": 2.3940873, 
     "_source": { 
      "ndex": 96, 
      "name": "Drowzee", 
      "date_captured": "2015-02-04", 
      "has_evolution": true, 
      "evolution": "Hypno", 
      "type": "Psychic", 
      "how_to_find": [ 
      { 
       "game": "Red", 
       "method": "Catch" 
      }, 
      { 
       "game": "Blue", 
       "method": "Catch" 
      }, 
      { 
       "game": "Gold", 
       "method": "Catch" 
      }, 
      { 
       "game": "Silver", 
       "method": "Catch" 
      } 
      ], 
      "height": 39, 
      "weight": 71.4 
     } 
     }, 
     { 
     "_index": "pokedex", 
     "_type": "pokemon", 
     "_id": "97", 
     "_score": 2.3940873, 
     "_source": { 
      "ndex": 97, 
      "name": "Hypno", 
      "date_captured": "2015-02-05", 
      "has_evolution": false, 
      "type": "Psychic", 
      "how_to_find": [ 
      { 
       "game": "Red", 
       "method": [ 
       "Catch", 
       "Evolve" 
       ] 
      }, 
      { 
       "game": "Blue", 
       "method": [ 
       "Catch", 
       "Evolve" 
       ] 
      }, 
      { 
       "game": "Gold", 
       "method": [ 
       "Catch", 
       "Evolve" 
       ] 
      }, 
      { 
       "game": "Silver", 
       "method": [ 
       "Catch", 
       "Evolve" 
       ] 
      } 
      ], 
      "height": 63, 
      "weight": 166.7 
     } 
     }, 
     { 
     "_index": "pokedex", 
     "_type": "pokemon", 
     "_id": "103", 
     "_score": 2.3940873, 
     "_source": { 
      "ndex": 103, 
      "name": "Exeggutor", 
      "date_captured": "2015-02-11", 
      "has_evolution": false, 
      "type": [ 
      "Grass", 
      "Psychic" 
      ], 
      "hm": { 
      "id": "HM04", 
      "name": "Strength" 
      }, 
      "how_to_find": [ 
      { 
       "game": "Red", 
       "method": "Evolve" 
      }, 
      { 
       "game": "Blue", 
       "method": "Evolve" 
      }, 
      { 
       "game": "Gold", 
       "method": "Evolve" 
      }, 
      { 
       "game": "Silver", 
       "method": "Evolve" 
      } 
      ], 
      "height": 79, 
      "weight": 264.6 
     } 
     }, 
     { 
     "_index": "pokedex", 
     "_type": "pokemon", 
     "_id": "122", 
     "_score": 2.3940873, 
     "_source": { 
      "ndex": 122, 
      "name": "Mr. Mime", 
      "date_captured": "2015-04-10", 
      "has_evolution": false, 
      "type": [ 
      "Psychic", 
      "Fairy" 
      ], 
      "how_to_find": [ 
      { 
       "game": "Red", 
       "method": "Event" 
      }, 
      { 
       "game": "Blue", 
       "method": "Event" 
      }, 
      { 
       "game": "Gold", 
       "method": [ 
       "Catch", 
       "Reward" 
       ] 
      }, 
      { 
       "game": "Silver", 
       "method": [ 
       "Catch", 
       "Reward" 
       ] 
      } 
      ], 
      "height": 51, 
      "weight": 120.1 
     } 
     }, 
     { 
     "_index": "pokedex", 
     "_type": "pokemon", 
     "_id": "124", 
     "_score": 2.3940873, 
     "_source": { 
      "ndex": 124, 
      "name": "Jynx", 
      "date_captured": "2015-04-12", 
      "has_evolution": false, 
      "type": [ 
      "Ice", 
      "Psychic" 
      ], 
      "how_to_find": [ 
      { 
       "game": "Red", 
       "method": "Event" 
      }, 
      { 
       "game": "Blue", 
       "method": "Event" 
      }, 
      { 
       "game": "Gold", 
       "method": "Catch" 
      }, 
      { 
       "game": "Silver", 
       "method": "Catch" 
      } 
      ], 
      "height": 55, 
      "weight": 89.5 
     } 
     }, 
     { 
     "_index": "pokedex", 
     "_type": "pokemon", 
     "_id": "16", 
     "_score": 2.1465569, 
     "_source": { 
      "ndex": 16, 
      "name": "Pidgey", 
      "date_captured": "2015-06-01", 
      "has_evolution": true, 
      "evolution": "Pidgeotto", 
      "type": [ 
      "Normal", 
      "Flying" 
      ], 
      "hm": { 
      "id": "HM02", 
      "name": "Fly" 
      }, 
      "how_to_find": [ 
      { 
       "game": "Red", 
       "method": "Catch" 
      }, 
      { 
       "game": "Blue", 
       "method": "Catch" 
      }, 
      { 
       "game": "Gold", 
       "method": "Catch" 
      }, 
      { 
       "game": "Silver", 
       "method": "Catch" 
      } 
      ], 
      "height": 12, 
      "weight": 4 
     } 
     }, 
     { 
     "_index": "pokedex", 
     "_type": "pokemon", 
     "_id": "18", 
     "_score": 2.1465569, 
     "_source": { 
      "ndex": 18, 
      "name": "Pidgeot", 
      "date_captured": "2015-06-03", 
      "has_evolution": false, 
      "type": [ 
      "Normal", 
      "Flying" 
      ], 
      "hm": { 
      "id": "HM02", 
      "name": "Fly" 
      }, 
      "how_to_find": [ 
      { 
       "game": "Red", 
       "method": "Evolve" 
      }, 
      { 
       "game": "Blue", 
       "method": "Evolve" 
      }, 
      { 
       "game": "Gold", 
       "method": "Evolve" 
      }, 
      { 
       "game": "Silver", 
       "method": "Evolve" 
      } 
      ], 
      "height": 59, 
      "weight": 87.1 
     } 
     }, 
     { 
     "_index": "pokedex", 
     "_type": "pokemon", 
     "_id": "41", 
     "_score": 2.1465569, 
     "_source": { 
      "ndex": 41, 
      "name": "Zubat", 
      "date_captured": "2015-09-01", 
      "has_evolution": true, 
      "evolution": "Golbat", 
      "type": [ 
      "Poison", 
      "Flying" 
      ], 
      "hm": { 
      "id": "HM02", 
      "name": "Fly" 
      }, 
      "how_to_find": [ 
      { 
       "game": "Red", 
       "method": "Catch" 
      }, 
      { 
       "game": "Blue", 
       "method": "Catch" 
      }, 
      { 
       "game": "Gold", 
       "method": "Catch" 
      }, 
      { 
       "game": "Silver", 
       "method": "Catch" 
      } 
      ], 
      "height": 31, 
      "weight": 16.5 
     } 
     }, 
     { 
     "_index": "pokedex", 
     "_type": "pokemon", 
     "_id": "84", 
     "_score": 2.1465569, 
     "_source": { 
      "ndex": 84, 
      "name": "Doduo", 
      "date_captured": "2015-01-04", 
      "has_evolution": true, 
      "evolution": "Dodrio", 
      "type": [ 
      "Normal", 
      "Flying" 
      ], 
      "hm": { 
      "id": "HM02", 
      "name": "Fly" 
      }, 
      "how_to_find": [ 
      { 
       "game": "Red", 
       "method": "Catch" 
      }, 
      { 
       "game": "Blue", 
       "method": "Catch" 
      }, 
      { 
       "game": "Gold", 
       "method": "Catch" 
      }, 
      { 
       "game": "Silver", 
       "method": "Catch" 
      } 
      ], 
      "height": 55, 
      "weight": 86.4 
     } 
     } 
    ] 
    }, 
    "aggregations": { 
    "My_Date_Ranges": { 
     "buckets": [ 
     { 
      "key": "*-2-3-2015", 
      "to": 1422972585150, 
      "to_as_string": "2-3-2015", 
      "doc_count": 3 
     }, 
     { 
      "key": "4-3-2015-6-3-2015", 
      "from": 1428070185150, 
      "from_as_string": "4-3-2015", 
      "to": 1433340585150, 
      "to_as_string": "6-3-2015", 
      "doc_count": 8 
     }, 
     { 
      "key": "6-3-2015-*", 
      "from": 1433340585150, 
      "from_as_string": "6-3-2015", 
      "doc_count": 17 
     } 
     ] 
    } 
    } 
} 

하지만 이러한 중첩 된 문서에서 아무것도를 가져 투쟁 - : 구조는 다소 다음과 같이합니다. 외부 정보 만 가져올 수 있습니다.

내 코드 : -

FileReader reader = new FileReader("varun_tmp.txt"); 
      JSONObject jsonObject; 
      try { 
       jsonObject = (JSONObject) new JSONParser().parse(reader); 

      long id = (Long) jsonObject.get("took"); 

      System.out.println("\nTesting:-\nTook : " + id); 

      JSONObject json_hits = (JSONObject) new JSONParser(). 

      JSONArray lang = (JSONArray) jsonObject.get("hits"); 

      for(int i=0; i<lang.size(); i++){ 
       System.out.println("The "+(i+1)+"th Element in Hits is : "+ lang.get(i)+"\n"); 
      } 

내가이 오류를 얻을 : - 스레드

예외를 "주"java.lang.ClassCastException가이 : org.json.simple.JSONObject는 캐스트 할 수없는 groupid.artifactid.App.main에서 org.json.simple.JSONArray (App.java:204)에

답변

2

그것은 당신의 JSON 구문 분석처럼 보인다 코드는 정확합니다. 그러나 탄성 검색이 응답을 구조화하는 방식으로 문제가 발생하고 있습니다.

루트 수준 "조회수"는 개체이며, 해당 개체의 내부에는 조회수라는 배열이 있습니다. 히트 개체를 얻으려면 한 번, 히트 개체 배열을 얻으려면 한 번 두 번 "히트"를 얻어야합니다.

JSONArray lang = (JSONArray) jsonObject.get("hits").get("hits");

:

이에게 기회를 줘