以下是响应正文:
{
"Updated_Fields": [
"a",
"b",
"c",
"d"
],
"Invalid_Fields": [
"cd",
"ab"
]
}
我要检查响应主体是否具有
JSONArray JSONResponseBody = new JSONArray(response.body().asString());
Assert.assertEquals(JSONResponseBody.getJSONObject(0).getString("Invalid_Fields"), "cd");
response.jsonPath().param("Invalid_Fields", "cd");
assertThat( response.asString(), hasJsonPath("Invalid_Fields.ab"));
遇到错误
答案 0 :(得分:0)
使用gson之类的库将String转换为Java对象,然后应用标准Java逻辑的一种方法(下面的示例)
私有静态最终列表INVALID_DATA = Arrays.asList(“ cd”,“ ab”);
public static void main(String[] args) { String input = "{ \"Updated_Fields\": [ \"a\", \"b\", \"c\", \"d\" ], \"Invalid_Fields\": [ \"cd\", \"ab\" ] }"; Gson gson = new Gson(); FieldData data = gson.fromJson(input, FieldData.class); System.out.println(isInvalidFields(data.Invalid_Fields)); } private static boolean isInvalidFields(List<String> Invalid_Fields) { if(CollectionUtils.isEmpty(Invalid_Fields) || Invalid_Fields.size() != 2) { return false; } return Invalid_Fields.containsAll(INVALID_DATA); }
到该数据的类映射的定义:
public class FieldData
{
public List<String> Updated_Fields;
public List<String> Invalid_Fields;
}