放心Groovy GPath过滤结果

放心Groovy GPath过滤结果,groovy,rest-assured,gpath,Groovy,Rest Assured,Gpath,我试图返回“DependentReferences”中“description”的值,其中响应满足两个条件:首先,检查名称是否为“John Smith”,然后检查首选项中的“image”是否等于“papaya.jpg” 答复如下: [ { "id": 1, "name": "John Smith", "description": null,

我试图返回“DependentReferences”中“description”的值,其中响应满足两个条件:首先,检查名称是否为“John Smith”,然后检查首选项中的“image”是否等于“papaya.jpg”

答复如下:

[    
    {
        "id": 1,
        "name": "John Smith",
        "description": null,
        "shortDescription": "No recorded interests.",
        "alias": "JS",
        "preferences": [
            {
                "id": 1,
                "description": "likes candy and papaya",
                "image": "papaya.jpg",
                "name": "Papaya",
                "dependentPreferences": [
                    {
                        "id": 1,
                        "description": "Fruit must be ripe",
                        "image": "ripe-papaya.jpg",
                        "name": "pap"
                    }
                ]
            }
         ]
    },
    {
        "id": 2,
        "name": "Jane Smith",
        "description": null,
        "shortDescription": "No recorded interests.",
        "alias": "JS",
        "preferences": [
            {
                "id": 1,
                "description": "likes candy and papaya",
                "image": "papaya.jpg",
                "name": "Papaya",
                "dependentPreferences": [
                    {
                        "id": 1,
                        "description": "Candy must be Skittles",
                        "image": "Skittles.jpg",
                        "name": "skt"
                    }
                ]
            }
         ]
    }
]
到目前为止,我已经尝试过这样的方法:

response.jsonPath().getString("find { it.name == 'John Smith' }.preferences.find {it.image == 'papaya.jpg'}.dependentPreferences.description 
但它在抱怨语法。我知道这部分代码可以找到图像:

response.jsonPath().getString("find { it.name == 'John Smith' }.preferences.image
但是我在构造第二个条件时遇到了困难。任何帮助都将不胜感激。

工作示例。这对我很有用:

def endpoint = "http://localhost:3130/ids.json"
def jsonResponse = get(endpoint).then().contentType(ContentType.JSON).extract().response()
def path = jsonResponse.jsonPath()

def result = path.getString("find { it.name == 'John Smith' }.preferences.find {it.image == 'papaya.jpg'}.dependentPreferences.description")

尽管说实话:在JSON中,
dependentReferences
是对象的数组,因此如果数据与呈现的数据不同,就有可能出错。

谢谢!事实上,尽管你的结果和我的答案完全一样,但你的评论给了我一个提示,让我明白我的代码失败的确切原因。就我而言,我的形象并不总是有价值的。使用静态键和值进行交换,帮助我解决了这个问题。