我收到以下格式的休息回复。
{
"query_result": {
"status": "SUCCESS","message": "Results Found","results": [
{
"key": "123"
},{
"key": "465"
}
]
}
}
从这个响应中,我想从键创建一个字符串列表。 因此希望获得如下列表结果:
["123","456"]
尝试按如下方式生成输出,但卡在以下代码块中的映射步骤。
public Optional<List<String>> get(HttpEntity<Request> request){
ResponseEntity<Response> response = getResponse(request); // this response holds above json data
return Optional.ofNullable(response)
.map(ResponseEntity::getBody)
.map(Response::getQueryResult)
.map(QueryResult::getResults)
.filter(CollectionUtils::isnotEmpty)
.map(results -> results.) // Stuck here. results here is the list of results shown in above json.
}
我无法从这里提取所有键的值。 我最终不得不按如下方式指示索引,这将不起作用,因为我想要所有键。
.map(results -> results.get(0))
尝试考虑使用迭代器,但最终只会捕获第一个键。
.filter(CollectionUtils::isnotEmpty)
.map(List::iterator)
.map(Iterator::next)
.map(result -> result.getKey())
我能帮我解决这个问题吗?谢谢。