【问题标题】:Rest Assured - Json Extract放心 - Json 提取物
【发布时间】:2018-12-07 08:25:23
【问题描述】:

我收到以下结构的响应:

[{
    "builds": [{
            "description": "JP MORGAN",
            "land": "FU",
            "companyCode": "1234",
            "accountNumber": "00000000000",
            "shortID4": "XYS"
        }
    ]
}

]

每当我想提取 JSON 属性时,例如描述,最终的对象不是我所期望的。它创建具有 ArrayString 的 List。

List<String> values = resp.path("builds.description");

每当我想测试/打印时,这不仅仅是因为结构的原因。

如何从这个 JSON 对象获取值到包含这些值的简单列表?谢谢

【问题讨论】:

    标签: rest-assured rest-assured-jsonpath


    【解决方案1】:

    希望以下其中一种解决方案可以解决您的问题。我建议你使用第一个。

    String responseJson= "[{     \"builds\": [{             \"description\": \"JP MORGAN\",         },      {             \"description\": \"JP MORGAN\",         }     ] }]";
    JsonPath jp = new JsonPath(responseJson);
    List<String> singleDestList = jp.getList("builds[0].description");
    System.out.println(singleDestList);
    

    或

    List<List<List<String>>> descriptionsList =jp.get("builds.description");
    List<Object> flatList = descriptionsList.stream().flatMap(List::stream).collect(Collectors.toList());       
    System.out.println(flatList.toString());
    

    或

    String[] descriptionArray = jp.get("builds.description").toString().replaceAll("\\[", "").replaceAll("]", "").split(",");
    List<String> descriptionList = Arrays.asList(descriptionArray);
    System.out.println(descriptionList);
    

    【讨论】:

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