JSON PATH 字段 NULL 检查表达式

JSON PATH Field NULL Checking Expression

我有一个 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}

我试图找到所有没有 likes 字段的图书价格。是否可以使用 Jayway Json Path library 查找。我正在尝试类似的东西 $.store.book[? (@.likes == null)].price 但我不知道是否有类似的东西?

这个 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

这是显示此读取工作的屏幕截图,使用 Jayway JsonPath Evaluator: