我试图返回"dependentPreferences“中的"description”值,其中响应满足两个条件:首先,检查名称是否为"John“,然后检查首选项中的”图像“是否等于"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但我在构造第二个条件时遇到了困难。任何帮助都将不胜感激。
发布于 2020-10-29 23:17:19
工作示例这里。这对我来说很管用:
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是--对象的数组,所以如果数据与显示的数据不同,就有可能出错。
https://stackoverflow.com/questions/64598049
复制相似问题