简体   繁体   English

Jayway JsonPath过滤谓词

[英]Jayway JsonPath filtering with predicates

I've recently taken up Jayway JsonPath and I've had trouble with how the inpath filtering works. 我最近开始从事Jayway JsonPath的工作,而在入口路径过滤的工作方式上遇到了麻烦。

So my JSON looks like this: 所以我的JSON看起来像这样:
At the top I have shareables. 在顶部,我有共享的东西。 These shareables have an array called user, which contains an ID and a name, and they also contain an item called dataset, which can contain any json. 这些可共享对象具有一个名为user的数组,其中包含一个ID和名称,并且还包含一个名为dataset的项,该项可以包含任何json。
These shareables can exist within the dataset as well. 这些可共享内容也可以存在于数据集中。
My working JSON looks like this: 我的工作JSON看起来像这样:

 {
    "shareable": {
        "user": [ 
            {
                "ID": 1,
                "Name": "Bob"
            },
            {
                "ID": 2,
                "Name": "Charles"
            }
        ],
        "dataSet": [
            { 
                "insulinMeasurement": 
                {
                    "timestamp": "Tuesday Morning",
                    "measurement": 174,
                    "unit": "pmol/L"
                } 
            },
            { 
                "insulinMeasurement": 
                {
                    "timestamp": "Tuesday Noon",
                    "measurement": 80,
                    "unit": "pmol/L"
                } 
            },
            { "shareable": {
                "user": [
                    { 
                        "ID": 3,
                        "Name": "Jim" 
                    }
                ],
                "dataSet": [
                    { 
                        "insulinMeasurement": 
                        {
                            "timestamp": "Tuesday Evening",
                            "measurement": 130,
                            "unit": "pmol/L"
                        } 
                    }
                ]
                }
            },
            { "unshareable": {
                "user": [ 
                    { 
                            "ID": 2,
                            "Name": "Bob"
                    }
                ],
                "dataSet": [
                    { 
                        "insulinMeasurement": 
                        {
                            "timestamp": "Tuesday Night",
                            "measurement": 130,
                            "unit": "pmol/L"
                        } 
                    }
                ]
                }
            }
        ]
    }
}  

So what I want is, all shareables that have a user with a certain ID. 所以我想要的是所有具有用户ID的共享对象。 So I figured the path I would use would look like this: 所以我想出了我要使用的路径,如下所示:

$..shareable[ ?(@.user[*].ID == 1 )]

which here has a hardcoded ID. 这里有一个硬编码的ID。 This returns nothing while 这什么都不返回

$..shareable[ ?(@.user[0].ID == 1 )]

returns any shareable where the first ID is 1. 返回第一个ID为1的任何可共享内容。
I also tried something along the lines of 我也尝试了一些类似的方法

$..shareable[ ?(@.user[?(@.ID == 1)]

which I figure should return any shareable that has a user with an ID of 1. 我认为应该返回ID为1的用户的任何可共享内容。
Am I going about this the wrong way? 我会以错误的方式处理吗? Do I need to somehow iterate through the user objects that exist? 我是否需要以某种方式遍历存在的用户对象?

Well, I figured it out, so if anyone stumbles across this, the query should look as follows: 好吧,我知道了,因此,如果有人偶然发现此问题,查询应如下所示:

$..shareable[?( " + user + " in @.user[*].ID )] $ .. shareable [?(@ .user [*]。ID)中的“ +用户+”

where user is just the int of the userId. 其中user只是userId的int。 Basically the right hand side creates a list of all IDs that shareable contains, and checks if the requested ID exists therein. 基本上,右侧会创建可共享包含的所有ID的列表,并检查其中是否存在所请求的ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM