gpt4 book ai didi

java - 使用 JSONAssert 查找 json diff 失败

转载 作者:行者123 更新时间:2023-12-01 16:30:37 26 4
gpt4 key购买 nike

我希望使用 Jackson 来查找 JSON diff,但它没有给出详细的错误消息。

所以我尝试使用 JSOnAssert 来查找两个 JSON 字符串之间的差异。

JSONAssert.assertEquals(expectedJsonResponse, actualJsonResponse, false);

遗憾的是,它似乎没有正确匹配,并且没有给出示例中的详细错误消息。如果你用过的话可以麻烦说一下吗?

java.lang.AssertionError: data[0] Could not find match for element {"errors":[{"httpStatus":"BAD_REQUEST","personId":null,"details":"User ID [UNKNOWN]. Invalid ID: NONSENSE"}],"successfulIds":["A0","B1","C3"]}
at org.skyscreamer.jsonassert.JSONAssert.assertEquals(JSONAssert.java:222)

实际 JSON:

{"_links":{"self":{"href":"https://myserver.com:1000/api/person/upload? myCsvFile={myCsvFile}","templated":true}},"data":[{"successfulIds":["A0","XYZ","C3"],"errors":[{"personId":null,"httpStatus":"BAD_REQUEST","details":"User ID [UNKNOWN]. Invalid ID: NONSENSE"}]}]}

预期 JSON:

{
"_links": {
"self": {
"href": "https://myserver.com:1000/api/person/upload?myCsvFile={myCsvFile}",
"templated": true
}
},
"data": [
{
"successfulIds": [
"A0",
"B1",
"C3"
],
"errors": [
{
"personId": null,
"httpStatus": "BAD_REQUEST",
"details": "User ID [UNKNOWN]. Invalid ID: NONSENSE"
}
]
}
]
}

最佳答案

我尝试通过电子邮件发送地址:http://jsonassert.skyscreamer.org/但得到了一个

The following message to jsonassert-dev@skyscreamer.org was undeliverable. The reason for the problem: 5.1.0 - Unknown address error 550-"5.1.1 The email account that you tried to reach does not exist

所以我尝试了ZJsonPatch。我喜欢这样一个事实:使用 jackson ,成员的顺序并不重要。换句话说,我首先尝试使用 Jackson 检查是否相等。 jackson 下令独立。然后,如果失败,我使用 ZJsonPatch 告诉我差异是什么。

{"op":"replace","path":"/data/0/successfulIds/1","value":"B9"}

它可以很好地处理嵌套的 JSON。

ObjectMapper mapper = new ObjectMapper();
JsonNode expected = mapper.readTree(expectedJsonResponse);
JsonNode actual = mapper.readTree(actualJsonResponse);

try {
assertEquals(expected, actual);
} catch (AssertionError ae) {
JsonNode patch = JsonDiff.asJson(actual, expected);
throw new Exception(patch.toString(), ae);
}

关于java - 使用 JSONAssert 查找 json diff 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62048635/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com