gpt4 book ai didi

java - 如何在没有元信息的情况下将json(来自elasticsearch)解析为数组

转载 作者:行者123 更新时间:2023-12-01 23:04:54 25 4
gpt4 key购买 nike

{
"took": 3,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1.0,
"hits": [
{
"_index": "contacts",
"_type": "index",
"_id": "2",
"_score": 1.0,
"_source": {
"id": "c201",
"name": "Johnny Depp",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000"
}
}
}
]
}
}

我有一个 elasticsearch json 对象要在我的应用程序中使用。我不想使用elasticsearch api。还需要去掉元信息,请您告知

最佳答案

此答案假设您正在使用 org.json包,并且 hits 数组只有一个元素(否则您需要循环 hitsArr):

JSONObject json = new JSONObject(jsonString);
JSONObject hitsObj = json.getJSONObject("hits");
JSONArray hitsArr = hitsObj.getJSONArray("hits");
JSONObject first = hitsArr.getJSONObject(0); // assumes 1 entry in hits array
JSONObject source = first.getJSONObject("_source");
JSONObject phone = source.getJSONObject("phone");

String id = source.getString("id");
String name = source.getString("name");
String mobile = phone.getString("mobile");
String home = phone.getString("home");

System.out.println(id + "\n" + name + "\n" + mobile + "\n" +home);

关于java - 如何在没有元信息的情况下将json(来自elasticsearch)解析为数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22933974/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com