Jayway JsonPath过滤谓词

时间:2018-02-13 16:40:16

标签: java jsonpath

我最近采用了Jayway JsonPath,我在使用inpath过滤方面遇到了麻烦。

所以我的JSON看起来像这样:
在顶部,我有可分享。这些可共享的数组有一个名为user的数组,其中包含一个ID和一个名称,它们还包含一个名为dataset的项,它可以包含任何json。
这些可共享也可以存在于数据集中 我工作的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"
                        } 
                    }
                ]
                }
            }
        ]
    }
}  

所以我想要的是,所有具有特定ID的用户的可共享。所以我认为我会使用的路径如下:

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

这里有一个硬编码的ID。

时没有返回任何内容
$..shareable[ ?(@.user[0].ID == 1 )]

返回第一个ID为1的任何可共享。
我也尝试了一些

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

我认为应该返回任何具有ID为1的用户的可共享 我是以错误的方式来做这件事的吗?我是否需要以某种方式遍历存在的用户对象?

1 个答案:

答案 0 :(得分:0)

Well, I figured it out, so if anyone stumbles across this, the query should look as follows:

$..shareable[?( " + user + " in @.user[*].ID )]

where user is just the int of the userId. Basically the right hand side creates a list of all IDs that shareable contains, and checks if the requested ID exists therein.

相关问题