json路径:数组fiilter结果的第一个元素
我不熟悉json路径:数组fiilter结果的第一个元素,json,jsonpath,json-path-expression,Json,Jsonpath,Json Path Expression,我不熟悉json路径,但我经常使用xpath。 我的问题是使用json path,提取previus过滤器生成的数组的nth元素。 例如,使用以下json { "store": { "book": [ { "category": "reference", "author": "Nigel Rees", "title": "Sayings of the C
json路径
,但我经常使用xpath
。
我的问题是使用json path
,提取previus过滤器生成的数组的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]
但它似乎不适用于任何在线评估工具
错误在哪里
提前感谢。您可以尝试在符合条件的列表中映射books数组,并从中获取第n个
元素。比如:
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));
List bookList=JsonPath.parse(您的_-json)。阅读(“$…book[?(@.category=='fraction')]”,List.class);
System.out.println(“打印第n本书=“+expensiveBooks.get(n));
是,对不起,我没有指定。在编程方面,我的解决方法与您建议的类似。我只是想知道是否有json路径语法能够直接提取值。谢天谢地,到目前为止,json路径语法还不支持这种查询。