gpt4 book ai didi

java - 如何获取两个 REST API 响应的差异?

转载 作者:行者123 更新时间:2023-12-01 18:42:52 25 4
gpt4 key购买 nike

我想比较 Java 中的两个 REST API 响应。目前我正在使用 zjsonpatch 库,通过它我可以获得差异,但差异包含其中一个 json 的值。我想要两个 json 节点。

请在下面找到我的代码:

    ObjectMapper jacksonObjectMapper = new ObjectMapper();
JsonNode beforeNode = jacksonObjectMapper.readTree(jsonActual);
JsonNode afterNode = jacksonObjectMapper.readTree(jsonCurrent);
JsonPatch patch = JsonDiff.asJsonPatch(beforeNode, afterNode);
String diffs = patch.toString();

下面是我得到的输出

op: replace; path: "/businessServiceabilityResponse/0/serviceabilityDetail/0/serviceabilitySource"; value: "ELOC"]

有没有办法可以获取两个不匹配的节点?

最佳答案

您可以尝试使用JSONAssert

JSONAssert.assertEquals(json1, json2, true);

json1:

[
{
studentname: "John",
john: "123"
},
{
studentname: "Ram",
Ram: "124"
}
]

json2

[
{
studentname: "Nhoj",
john: "123"
},
{
studentname: "Ram",
Ram: "124"
}
]

输出:

Exception in thread "main" java.lang.AssertionError: [0].studentname
Expected: john
got: Nhoj

at org.skyscreamer.jsonassert.JSONAssert.assertEquals(JSONAssert.java:417)
at org.skyscreamer.jsonassert.JSONAssert.assertEquals(JSONAssert.java:394)
at org.skyscreamer.jsonassert.JSONAssert.assertEquals(JSONAssert.java:336)
at com.smilep.java.thirteen.so.JSONAssertExample.main(JSONAssertExample.java:32)

关于java - 如何获取两个 REST API 响应的差异?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59887511/

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