JSON PATH字段空值检查表达式

9

I have a json array like bellow :

{
"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}

我想查找所有没有likes字段的书籍价格。是否可以使用Jayway Json Path库进行查找?我正在尝试类似于这样的查询$.store.book[? (@.likes == null)].price,但我不知道是否有类似的功能。


你可以使用 http://jsonpath.com 进行验证。 - Anuruddha
1个回答

9

这个 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");

更多细节请参阅文档

下面是一个屏幕截图,展示了使用Jayway JsonPath Evaluator的读取过程:

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接