如何组合来自 json 的两个属性以在 Java 中使用 in 子句创建 json 谓词?

问题描述

我有一个如下的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,"author": "Herman Melville","title": "Moby Dick","isbn": "0-553-21311-3","price": 8.99,"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)])]

但是上面的谓词不起作用。有什么办法可以实现吗?

解决方法

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

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

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

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