1

我有一个如下的json

{
    "store": {
        "book": [
            {
                "category": "reference",
                "author": "Nigel Rees",
                "title": "Sayings of the Century",
                "price": 8.95,
               "version": 1
            },
            {
                "category": "reference",
                "author": "Nigel Reas",
                "title": "Sayings of the Decade",
                "price": 8.96,
               "version": 2
            },
            {
                "category": "fiction",
                "author": "Evelyn Waugh",
                "title": "Sword of Honour",
                "price": 12.99,
                "version": 1
            },
            {
                "category": "fiction",
                "author": "Herman Melville",
                "title": "Moby Dick",
                "isbn": "0-553-21311-3",
                "price": 8.99,
                "version": 2
            },
            {
                "category": "fiction",
                "author": "J. R. R. Tolkien",
                "title": "The Lord of the Rings",
                "isbn": "0-395-19395-8",
                "price": 22.99,
                "version": 3
            }
        ],
        "bicycle": {
            "color": "red",
            "price": 19.95
        }
    },
    "expensive": 10
}

现在我想根据两个属性过滤 json。

例如:$.store.book[?((@.category,@.version) in [('fiction',2),('reference',1)])]

但上述谓词不起作用。有没有办法做到这一点?

4

1 回答 1

0

您显示的语法看起来不正确。据我了解 JayWay 实现(是的,这归结为库的实现细节),您需要依次使用和组合过滤器&&||如下所示:

$.store.book[?(@.category=='fiction' && @.version=='1' || @.category=='reference' && @.version=='2')]

首先,我尝试使用 r 创建组合过滤器in,但这似乎选择了比您想要的更多的值:

$.store.book[?(@.category in  ['fiction','reference'] && @.version in  ['1','2'])]
于 2021-06-10T20:01:44.570 回答