如何结合 json 中的两个属性以在 Java 中的子句中创建 json 谓词?
How to combine two attribute from a json to create json predicate with in clause in Java?
我有一个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)])]
但是上面的谓词不起作用。有什么办法可以实现吗?
您显示的语法看起来不正确。据我了解 JayWay 实现(是的,这归结为库的实现细节),您需要按顺序使用 &&
和 ||
组合过滤器,如下所示:
$.store.book[?(@.category=='fiction' && @.version=='1' || @.category=='reference' && @.version=='2')]
首先,我尝试使用 in
创建组合过滤器,但这似乎 select 比您想要的值多:
$.store.book[?(@.category in ['fiction','reference'] && @.version in ['1','2'])]
我有一个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)])]
但是上面的谓词不起作用。有什么办法可以实现吗?
您显示的语法看起来不正确。据我了解 JayWay 实现(是的,这归结为库的实现细节),您需要按顺序使用 &&
和 ||
组合过滤器,如下所示:
$.store.book[?(@.category=='fiction' && @.version=='1' || @.category=='reference' && @.version=='2')]
首先,我尝试使用 in
创建组合过滤器,但这似乎 select 比您想要的值多:
$.store.book[?(@.category in ['fiction','reference'] && @.version in ['1','2'])]