I have the following json returned from Elasticsearch. What is the fastest and most efficient way to retrieve the highlight.name
value into a List<String>
, in Java?
我从Elasticsearch返回了以下json。在Java中,将highlight.name值检索到List
{
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"max_score": 6.512624,
"hits": [
{
"_index": "shops",
"_type": "shop",
"_id": "AVfRy7_-rRRgqtjX0fQt",
"_score": 6.512624,
"highlight": {
"name": [
" <em>Smartwatch</em> Phone"
]
}
},
{
"_index": "shops",
"_type": "shop",
"_id": "AVfRy7_9rRRgqtjX0fGz",
"_score": 6.446859,
"highlight": {
"name": [
" <em>Smartwatch</em>"
]
}
},
{
"_index": "shops",
"_type": "shop",
"_id": "AVfRy7_-rRRgqtjX0fVa",
"_score": 3.7999475,
"highlight": {
"name": [
" 3G <em>Smartphone</em>"
]
}
}
]
}
}
4 个解决方案
#1
1
Use JSONObject for that:
使用JSONObject:
// jsonResult is a string that contains your ES response
JSONArray json = (new JSONObject(jsonResult)).getJSONObject("hits").getJSONArray("hits");
List<JSONArray> result = new ArrayList<>();
json.forEach((j) -> {
JSONObject highlight = ((JSONObject) j).getJSONObject("highlight");
result.add(highlight.getJSONArray("name"));
});
// Outputs [[" <em>Smartwatch<\/em> Phone"], [" <em>Smartwatch<\/em>"], [" 3G <em>Smartphone<\/em>"]]
System.out.println(result);
#2
0
Jackson libraries provide json simple format.
杰克逊图书馆提供简单的json格式。
ObjectMapper om = new ObjectMapper();
om.enable(SerializationConfig.Feature.INDENT_OUTPUT);
Object json = om.readValue( output, Object.class );
System.out.println( objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(json));
#3
0
You will have to benchmark the alternatives to see which is faster and more "efficient" (whatever that means) for your use case. The following may be faster then going via Jackson but is more fragile given the use of a regular expression.
您将不得不对备选方案进行基准测试,以确定哪种方法更快,更有效“(无论这意味着什么)用于您的用例。以下可能比通过杰克逊更快,但由于使用正则表达式更脆弱。
List<String> matches = new ArrayList<>();
Pattern p = Pattern.compile("\"highlight\".*?\"name\": \\[\\s*\"(.*?)\"\\s*\\]", Pattern.DOTALL);
Matcher m = p.matcher(json);
while (m.find()) {
matches.add(m.group(1));
}
System.err.println(matches);
#4
0
you can use jsonPath library, it's fastest way to read a value from json just in one line of code.
你可以使用jsonPath库,它是在一行代码中从json读取值的最快方法。
List<String> highlightResults = JsonPath.parse(/*yourJsonString*/).read("$..name", List.class);
#1
1
Use JSONObject for that:
使用JSONObject:
// jsonResult is a string that contains your ES response
JSONArray json = (new JSONObject(jsonResult)).getJSONObject("hits").getJSONArray("hits");
List<JSONArray> result = new ArrayList<>();
json.forEach((j) -> {
JSONObject highlight = ((JSONObject) j).getJSONObject("highlight");
result.add(highlight.getJSONArray("name"));
});
// Outputs [[" <em>Smartwatch<\/em> Phone"], [" <em>Smartwatch<\/em>"], [" 3G <em>Smartphone<\/em>"]]
System.out.println(result);
#2
0
Jackson libraries provide json simple format.
杰克逊图书馆提供简单的json格式。
ObjectMapper om = new ObjectMapper();
om.enable(SerializationConfig.Feature.INDENT_OUTPUT);
Object json = om.readValue( output, Object.class );
System.out.println( objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(json));
#3
0
You will have to benchmark the alternatives to see which is faster and more "efficient" (whatever that means) for your use case. The following may be faster then going via Jackson but is more fragile given the use of a regular expression.
您将不得不对备选方案进行基准测试,以确定哪种方法更快,更有效“(无论这意味着什么)用于您的用例。以下可能比通过杰克逊更快,但由于使用正则表达式更脆弱。
List<String> matches = new ArrayList<>();
Pattern p = Pattern.compile("\"highlight\".*?\"name\": \\[\\s*\"(.*?)\"\\s*\\]", Pattern.DOTALL);
Matcher m = p.matcher(json);
while (m.find()) {
matches.add(m.group(1));
}
System.err.println(matches);
#4
0
you can use jsonPath library, it's fastest way to read a value from json just in one line of code.
你可以使用jsonPath库,它是在一行代码中从json读取值的最快方法。
List<String> highlightResults = JsonPath.parse(/*yourJsonString*/).read("$..name", List.class);