get api and compare in jason library

142 阅读1分钟
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.13.0</version>
</dependency>

<dependency>
    <groupId>org.skyscreamer</groupId>
    <artifactId>jsonassert</artifactId>
    <version>1.5.0</version>
    <scope>test</scope>
</dependency>

JSONAssert 库提供了在 JSON 比较期间忽略元素顺序的选项。下面是一个更新的示例,演示了如何在忽略元素顺序的情况下执行 JSON 比较:


```import com.fasterxml.jackson.databind.ObjectMapper;
import com.microsoft.playwright.*;
import org.skyscreamer.jsonassert.JSONAssert;
import org.skyscreamer.jsonassert.JSONCompareMode;

public class PlaywrightExample {
    public static void main(String[] args) {
        try (Playwright playwright = Playwright.create()) {
            Browser browser = playwright.chromium().launch();
            Page page = browser.newPage();

            // Navigate to the login page and perform login actions

            // Check API response after login
            page.navigate("https://example.com/api/apple");
            Response response = page.waitForResponse("**/api/apple");
            String responseBody = response.text();

            // Deserialize API response into ApiResponse
            ObjectMapper objectMapper = new ObjectMapper();
            ApiResponse apiResponse = objectMapper.readValue(responseBody, ApiResponse.class);

            // Create an instance of your own bean
            MyBean myBean = new MyBean();
            myBean.setId("123");
            myBean.setName("Apple");

            // Convert objects to JSON strings
            String expectedJson = objectMapper.writeValueAsString(myBean);
            String actualJson = objectMapper.writeValueAsString(apiResponse);

            // Perform JSON comparison while ignoring order
            JSONAssert.assertEquals(expectedJson, actualJson, JSONCompareMode.LENIENT);

            browser.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

import com.fasterxml.jackson.databind.ObjectMapper;
import com.microsoft.playwright.*;
import org.skyscreamer.jsonassert.JSONAssert;
import org.skyscreamer.jsonassert.JSONCompareMode;

public class PlaywrightExample {
    public static void main(String[] args) {
        try (Playwright playwright = Playwright.create()) {
            Browser browser = playwright.chromium().launch();
            Page page = browser.newPage();

            // Navigate to the login page and perform login actions

            // Check API response after login
            page.navigate("https://example.com/api/apple");
            Response response = page.waitForResponse("**/api/apple");
            String responseBody = response.text();

            // Deserialize API response into ApiResponse
            ObjectMapper objectMapper = new ObjectMapper();
            ApiResponse apiResponse = objectMapper.readValue(responseBody, ApiResponse.class);

            // Create an instance of your own bean
            MyBean myBean = new MyBean();
            myBean.setId("123");
            myBean.setName("Apple");

            // Convert objects to JSON strings
            String expectedJson = objectMapper.writeValueAsString(myBean);
            String actualJson = objectMapper.writeValueAsString(apiResponse);

            // Perform JSON comparison while ignoring order
            JSONAssert.assertEquals(expectedJson, actualJson, JSONCompareMode.LENIENT);

            browser.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

在这个更新的示例中,我们在调用 JSONAssert.assertEquals 方法时使用 JSONCompareMode.LENIENT 选项。此模式允许在不考虑顺序的情况下比较 JSON 结构中的元素。

通过使用 JSONCompareMode.LENIENT,JSONAssert 库将比较 JSON 结构和值,同时忽略元素的顺序。

请记住导入 JSONCompareMode 枚举并更新 ApiResponse 和 MyBean 类中的类和属性名称,以匹配您的实际响应结构和您自己的 bean 结构。

请注意,在 JSON 比较期间忽略元素的顺序可能并不适用于所有场景,因为元素的顺序在某些情况下可以传达重要信息。请谨慎使用此方法,同时考虑数据的具体要求和特征。