首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用Java解析嵌套JSON

用Java解析嵌套JSON
EN

Stack Overflow用户
提问于 2017-02-03 14:15:50
回答 1查看 519关注 0票数 1

我对JSON有点陌生,希望获取elasticsearch返回给我的值。结构有点像这样:

代码语言:javascript
复制
{
  "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
        }
      ]
    }
  }
}

我尝试使用"org.json.simple.*“库,但是它很难从这样的嵌套文档中获取任何东西。只能获取外部信息。

我的代码:-

代码语言:javascript
复制
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");
            }

我知道这个错误:-

线程"main“java.lang.ClassCastException中的异常:不能将org.json.simple.JSONObject转换为org.json.simple.JSONArray at groupid.artifactid.App.main(App.java:204)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-02-03 17:19:44

看起来您的JSON解析代码是正确的-但是您会被弹性搜索构造它们的响应的方式所绊倒。

根级" hits“是一个对象,该对象的内部是一个名为hits的数组。您需要获得两次“命中”-一次获得hits对象,然后一次获得实际的命中数组。

试试看:

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42026282

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档