Java 比较JSON忽略ID值?

Java 比较JSON忽略ID值?,java,json,compare,assert,Java,Json,Compare,Assert,我使用JSON单元来比较两个JSON响应。 然而,UUID每次都是动态的,因此响应总是不同的,下面的内容不会忽略“id”,并且总是将其标记为不同 private void assertWithIgnore(String endpoint, String expected, int code) throws IOException { try { response.then() .statusCode(code)

我使用JSON单元来比较两个JSON响应。 然而,UUID每次都是动态的,因此响应总是不同的,下面的内容不会忽略“id”,并且总是将其标记为不同

private void assertWithIgnore(String endpoint, String expected, int code) throws IOException {
    try {
        response.then()
                .statusCode(code)
                .contentType(ContentType.JSON);
        if (compare) {
            assertJsonEquals(response.asString(),
                    resource("expected/" + expected),
                    JsonAssert.whenIgnoringPaths("item[*].id"));
        } else {
            Assert.fail("Not comparing response, write direct to file!");
        }
    } catch (AssertionError error) {
        FileUtils.writeStringToFile(getResultFile(expected), response.prettyPrint());
        failures.put(baseURL + basePath + endpoint, response);
        throw error;
    }
}
下面是JSON的一个小示例:

{
"item": [
{
"id": "1",
"title": "Hello"
}
]
}

用以下方法解决了上述问题:

JsonAssert.setOptions(IGNORING_VALUES);

有了JsonAssert,这也应该可以…准备好了吗

ArrayValueMatcher<Object> arrayValueMatcher = new ArrayValueMatcher<>(new CustomComparator(JSONCompareMode.LENIENT, new Customization("item[*].id", new ValueMatcher<Object>() {
                    @Override
                    public boolean equal(Object o1, Object o2) {
                        return true;
                    }
                })));

Customization arrayValueMatcherCustomization = new Customization("item", arrayValueMatcher);


CustomComparator customArrayValueComparator = new CustomComparator(JSONCompareMode.LENIENT, arrayValueMatcherCustomization);


JSONAssert.assertEquals(expect, actual, customArrayValueComparator);
ArrayValueMatcher ArrayValueMatcher=新的ArrayValueMatcher(新的CustomComparator(JSONCompareMode.LENIENT,新的自定义(“item[*].id”,新的ValueMatcher()){
@凌驾
公共布尔相等(对象o1、对象o2){
返回true;
}
})));
定制arrayValueMatcher定制=新定制(“项目”,arrayValueMatcher);
CustomComparator customArrayValueComparator=新的CustomComparator(JSONCompareMode.LENIENT,arrayValueMatcherCustomization);
assertEquals(expect、actual、customArrayValueComparator);

我正在使用库版本1.5.0

请确保您使用的是最新版本的
JsonUnit
dependency&还可以看看这篇文章,哪篇文章?我使用的是1.29.1版,点击我超链接的帖子。我在这里尝试了解决方案。。仍然会将这些ID视为不同。