数组上的JsonPath AND运算符 [英] JsonPath AND Operator on Array

查看:136
本文介绍了数组上的JsonPath AND运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JSON数组,我想对其应用两个过滤器:

I have an array in JSON and I would like to apply two filters to it like so:

$._embedded.values[0]._embedded.data[?(@.var1='value1' && @.var2='value2')]

我只需要从满足AND运算的数组中选择那些元素.但是,实际上这似乎行不通.

I need to select only those elements from the array that satisfies the AND operation. However, in practice this doesn't appear to work.

是否可以执行此操作,还是必须执行两步操作来提取一组,然后再次过滤以获得最终结果?

Is it possible to do this or must I perform a two step action to extract one set then filter again to get my final results?

推荐答案

据我所知,尚不支持AND/OR.但是,您可以相对接近此[,]数组.但这甚至让我怀疑何时进行测试.有一些奇怪的行为.我从 Jayway JsonPath Evaluator 中获取了一个JSON字符串示例,并将其放入

As far as I know AND / OR are not supported yet. But you can get relatively close with this [,] array. But even this made me wonder when I tested it. There's some strange behaviour. I took an example JSON string from the Jayway JsonPath Evaluator and put it into JsonPath Expression Tester (because it didn't work with Jayway when I tried).

所以我用curiousconcept版本测试过的JSON是:

So the JSON I've tested with the curiousconcept version is:

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

表达式:

$..book[?(@.price==8.99),?(@.category=='fiction')]

其结果是:

[  
   {  
      "category":"fiction",
      "author":"Herman Melville",
      "title":"Moby Dick",
      "isbn":"0-553-21311-3",
      "price":8.99
   }
]

什么似乎是完美的. (价格为8.99,类别为小说" ...完美!)

what seems to be perfect. (take the price 8.99 AND category 'fiction'... perfect!)

但是:将其更改为:

$..book[?(@.category=='fiction'),?(@.price==8.99)]

然后输出为:

[
]

在我看来,[,]在Jayway JsonPath Tester以及curiousconcept.com表达测试器中没有很好地实现.

It seems to me that [,] is not well implemented in Jayway JsonPath Tester as well as curiousconcept.com expression tester.

但是据我所知,他们正在对AND和/或(issue27

But from what I know they're working on an (real) implementation of AND and or (issue27 here at google code).

希望这至少可以澄清一些东西!

Hope this clarifies at least something!

这篇关于数组上的JsonPath AND运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆