【发布时间】:2019-10-21 22:28:24
【问题描述】:
我有以下 JSON 响应匿名正文,我需要在 groovy 的闭包中使用 find 或 findAll 动态解析嵌套数组以根据条件检索键的值
[
{
"children": [
{
"attr": {
"reportId": "1",
"reportShortName": "ABC",
"description": "test,
}
},
{
"attr": {
"reportId": "2",
"reportShortName": "XYZ",
"description": "test",
}
}
}
]
我尝试了以下方法,但未能从 JSON 响应中检索到 reportId 键的值
package com.src.test.api;
import static io.restassured.RestAssured.given;
import io.restassured.path.json.JsonPath;
import io.restassured.response.Response;
public class GetReportId {
public void getReportId(String reportName) throws Exception {
String searchReports = "http://localhost:8080/reports";
Response resp=given().request().when().get(searchReports).then().extract().response();
JsonPath jsonPath = new JsonPath(resp.asString());
String reportId1 =jsonPath.get("$.find{it.children.contains(restAssuredJsonRootObject.$.children.find{it.attr.reportShortName == 'ABC'})}.attr.reportId");
String reportId2 = jsonPath.get("$.find{it.children.attr.reportShortName.contains(restAssuredJsonRootObject.$.children.find{it.attr.reportShortName.equals('XYZ')}.attr.reportShortName)}.attr.reportId");
System.out.println("ReportId: " + reportId1);
}
}
父匿名数组中可能有多个 JSON 对象,需要使用 groovy 闭包中的 find 或 findAll 来获取 reportId
需要获取reportId,但似乎有问题。任何帮助将不胜感激。
【问题讨论】:
-
有什么理由不能使用 Groovy 自己的 JSON 工具?
new JsonSlurper().parseText(jsonText)*.children*.attr*.findAll{it.reportShortName=='ABC'}*.reportId.flatten() -
谢谢!不像我们试图按照框架坚持使用 REST-Assured 库
标签: groovy rest-assured jsonpath rest-assured-jsonpath gpath