0

使用 RestAssured 我正在尝试比较两个 JSON 对象。

示例:第一个 JSON 来自 excel 我将其作为字符串读取并存储

String a = "Some JSON from Excel and I store it as a String";

第二个 JSON 是返回响应对象的响应,该对象实际上是 JSON

Response expectedResponse = RestAssured.given().contentType("application/json").header(auth).get(endpoint).then().contentType("application/json").extract().response();

我想将这两个作为 JSON 对象进行比较,因为当我将响应转换为 String 并尝试进行比较时,如果 JSON 模式的顺序发生变化,我的断言就会失败。

我尝试了将字符串转换为 JSON 的方法,但找不到任何方法。有人可以帮我解决这个问题

4

2 回答 2

1

如果字段顺序不确定,那么我建议您使用 Hamcrest Matchers

你还没有发布回复,所以我只能给你举例

body(containsString("Hello World"));

或者您也可以尝试以下方法

body("find { it.userId == '123' }.subject", containsInAnyOrder("MATHS", "SCIENCE"))
于 2018-09-24T08:32:52.610 回答
0

您可以使用 objectMapper 来实现您要查找的内容,如果您的对象中的属性顺序不同,它不会失败,请查看以下示例:

    String s1 = "{ \"employee\": { \"id\": \"1212\", \"fullName\": \"John Miles\", 
    \"age\": 34 } }";
    String s2 = "{ \"employee\": { \"id\": \"1212\", \"age\": 34, \"fullName\": \"John 
    Miles\" } }";
    ObjectMapper mapper = new ObjectMapper();
    JsonNode s1Json = mapper.readTree(s1);
    JsonNode s2Json = mapper.readTree(s2);
    System.out.println(s1Json.equals(s2Json));
于 2020-01-02T15:28:59.353 回答