我刚接触json-path,但我经常使用xpath。我的问题是用json-path提取前一个过滤器产生的数组的nth元素。例如,使用以下json
{
"store": {
"book": [
{
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
},
{
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
},
{
"category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
},
{
"category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
},
"expensive": 10
}我想将第一本书与“小说”类别相匹配。
我试着用
$..book[?(@.category=='fiction')][0]但似乎它与任何在线评估工具都不起作用。
错误在哪里?
提前谢谢。
发布于 2018-08-09 18:36:28
您可以尝试将books数组映射到满足条件的列表中,并从中获取nth元素。像这样:
List<Map<String, Object>> bookList = JsonPath.parse(your_json).read("$..book[?(@.category=='fiction')]", List.class);
System.out.println("Printing the nth book = "+expensiveBooks.get(n));https://stackoverflow.com/questions/51707036
复制相似问题