在 JSONPath 0.9.1 中,以下 Json 路径是有效的:
http://jsonpath.herokuapp.com/?path= $.store.book[?(@.author==%27Nigel%20Rees%27)][0]
回归
{
"category" : "reference",
"author" : "Nigel Rees",
"title" : "Sayings of the Century",
"price" : 8.95
}
我已经升级到最新版本 (2.3),查询现在返回空数组。
这是错误还是从结果数组中检索元素的方式发生了变化?
最佳答案
鉴于此文档:
{
"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
}
使用 JsonPath 2.3.0,以下代码返回 JSONArray
(而不是 Object[]
):
JsonPath.parse(JSON).read("$.store.book[?(@.author==\"Nigel Rees\")]");
所以,下面的代码...
JSONArray read = JsonPath.parse(JSON).read("$.store.book[?(@.author==\"Nigel Rees\")]");
System.out.println(read.get(0));
... 将打印:
{category=reference, author=Nigel Rees, title=Sayings of the Century, price=8.95}
关于arrays - 应用谓词后从 JSONPath 数组结果中获取特定对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47572081/