Get specific object from JSONPath array result after predicate is applied
在 JSONPath 0.9.1 中,以下 Json 路径有效:
http://jsonpath.herokuapp.com/?path=$.store.book[?(@.author==\\'Nigel Rees\\')][0]
返回
1 2 3 4 5 6 | { "category" :"reference", "author" :"Nigel Rees", "title" :"Sayings of the Century", "price" : 8.95 } |
我已升级到最新版本 (2.3),现在查询返回空数组。
这是一个错误还是从结果数组中检索元素的方式发生了变化?
鉴于此文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | { "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,以下代码返回
1 | JsonPath.parse(JSON).read("$.store.book[?(@.author=="Nigel Rees")]"); |
所以,下面的代码...
1 2 | JSONArray read = JsonPath.parse(JSON).read("$.store.book[?(@.author=="Nigel Rees")]"); System.out.println(read.get(0)); |
... 将打印:
1 | {category=reference, author=Nigel Rees, title=Sayings of the Century, price=8.95} |