【问题标题】:How to search in anonymous and nested array using find or findAll in groovy's closures using REST-Assured library?如何使用 REST-Assured 库在 groovy 闭包中使用 find 和 findAll 在匿名和嵌套数组中搜索?
【发布时间】:2019-10-21 22:28:24
【问题描述】:

我有以下 JSON 响应匿名正文,我需要在 groovy 的闭包中使用 findfindAll 动态解析嵌套数组以根据条件检索键的值

[
{
  "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


【解决方案1】:

假设你想要所有的reportIds

List<String> reportIds = jsonPath.get("children.flatten().attr.reportId");

会给你你想要的,即使父匿名数组有多个条目。

我使用以下 JSON 进行了测试

[
  {
    "children": [
      {
        "attr": {
          "reportId": "1",
          "reportShortName": "ABC",
          "description": "test"
        }
      },
      {
        "attr": {
          "reportId": "2",
          "reportShortName": "XYZ",
          "description": "test"
        }
      }
    ]
  },
  {
    "children": [
      {
        "attr": {
          "reportId": "3",
          "reportShortName": "DEF",
          "description": "test"
        }
      },
      {
        "attr": {
          "reportId": "4",
          "reportShortName": "IJK",
          "description": "test"
        }
      }
    ]
  }
]

它给了我["1", "2", "3", "4"],即所有孩子的reportIds

如果您知道要查找的 reportId 的索引,则可以像这样使用它:

String reportId = jsonPath.get("children.flatten().attr.reportId[0]");

如果您要查找特定报告的 reportId,您也可以这样做:

String reportId = jsonPath.get("children.flatten().attr.find{it.reportShortName == 'ABC'}.reportId")

会给你"1"

注意:您分配结果的变量的类型对于类型推断和强制转换很重要。例如,你不能这样做:

String [] reportIds = jsonPath.get("children.flatten().attr.reportId");

int reportId = jsonPath.get("children.flatten().attr.reportId[0]");

这两件事都会抛出 ClassCastException。

【讨论】:

    猜你喜欢
    • 2018-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-05
    相关资源
    最近更新 更多