2017-02-03 10 views
1

私はJSONには少し新しく、elasticsearchによって返された値を取得したいと思っています。構造がある程度このようなものです: - 「* org.json.simple」を入れ子になったJSONのJavaでの解析

{ 
    "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の構文解析のように見えますコードは正しいですが、あなたは弾力的な検索が彼らの反応を構造化する方法でうまくいっています。

ルートレベルの "ヒット"はオブジェクトであり、そのオブジェクトの内部にヒットと呼ばれる配列があります。ヒットオブジェクトを取得するために1回、次にヒットの実際の配列を取得するために1回、「ヒット」を2回取得する必要があります。

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

この打撃を与えます

関連する問題