首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >请放心,Groovy GPath过滤结果

请放心,Groovy GPath过滤结果
EN

Stack Overflow用户
提问于 2020-10-29 19:34:39
回答 1查看 906关注 0票数 0

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

响应机构如下:

代码语言:javascript
复制
[    
    {
        "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"
                    }
                ]
            }
         ]
    }
]

到目前为止,我已经尝试过这样的方法:

代码语言:javascript
复制
response.jsonPath().getString("find { it.name == 'John Smith' }.preferences.find {it.image == 'papaya.jpg'}.dependentPreferences.description 

但这是在抱怨语法。我知道这部分代码可以找到图像:

代码语言:javascript
复制
response.jsonPath().getString("find { it.name == 'John Smith' }.preferences.image

但我在构造第二个条件时遇到了困难。任何帮助都将不胜感激。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-10-29 23:17:19

工作示例这里。这对我来说很管用:

代码语言:javascript
复制
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中,dependentPreferences--对象的数组,所以如果数据与显示的数据不同,就有可能出错。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64598049

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档