我有一个类似于下面的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,
"likes": 1
},
{
"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}
我试图找到没有赞字段的所有图书价格。是否可以使用Jayway Json Path library找到。我正在尝试这样的事情$.store.book[? (@.likes == null)].price
,但我不知道是否有类似的东西?
答案 0 :(得分:2)
这个jsonpath表达式是正确的:
$.store.book[?(@.likes == null)].price
但您必须设置DEFAULT_PATH_LEAF_TO_NULL
,例如:
Configuration conf = Configuration.defaultConfiguration()
.addOptions(Option.DEFAULT_PATH_LEAF_TO_NULL);
String pricesForBooksWithNoLikes = JsonPath.using(conf).parse(json)
.read("$.store.book[?(@.likes == null)].price");
更多详情in the docs。
显示此阅读效果的屏幕截图