如果我有
{
"took": 4,
"timed_out": false,
"hits": {
"total": 14,
"max_score": 1,
"hits": [
{
"_index": "someIndex",
"_source": {
"id": "de",
"title": "something 1"
},
"_index": "someIndex",
"_source": {
"id": "def",
"title": "something 1"
}
}
]
}
}
如何使用简单的 ObjectMapper
方法仅提取源列表?我不想自己处理每个节点。
最佳答案
我想有效的 json 是这样的
{
"took": 4,
"timed_out": false,
"hits": {
"total": 14,
"max_score": 1,
"hits": [
{
"_index": "someIndex",
"_source": {
"id": "de",
"title": "something 1"
}
},
{
"_index": "someIndex",
"_source": {
"id": "def",
"title": "something 1"
}
}
]
}
}
如果是这样,你可以得到它
ObjectMapper mapper = new ObjectMapper();
JsonNode data = mapper.readTree(json);
data.findValues("_source"); // [{"id":"de","title":"something 1"}, {"id":"def","title":"something 1"}]
//If you want to convert it to custom object
List<Source> sources = mapper.convertValue(data.findValues("_source"), new TypeReference<List<Source>>() {});
关于java - Java中的Json : How to extract list of subelements?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25203429/