放心-杰森摘录

时间:2018-06-28 09:48:46

标签: rest-assured rest-assured-jsonpath

我收到以下结构的回复:

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

]

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

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

每当我要测试/打印时,由于结构原因,这都不是完全可能的。 enter image description here

如何从此JSON对象获取值到包含值的简单List?谢谢

1 个答案:

答案 0 :(得分:0)

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

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);

OR

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());

OR

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