gpt4 book ai didi

java - 当响应顺序不同时如何比较两个 JSon 响应

转载 作者:行者123 更新时间:2023-12-04 07:54:56 25 4
gpt4 key购买 nike

下面是我收到的两个 API 调用的响应,响应是相同的,但数据的顺序是:

{"URI":"CoGroup/2","level":"total","name":null,"code":null,"baseColor":null,"secondaryColor":null,"selected":null,"value":317978,"Byyear":[],"count":10499},
{"URI":"integer/7005","level":"total","name":null,"code":null,"baseColor":null,"secondaryColor":null,"selected":null,"value":26857,"Byyear":[],"count":4542},
{"URI":"intgroup/78","level":"total","name":null,"code":null,"baseColor":null,"secondaryColor":null,"selected":null,"value":105304,"Byyear":[],"count":1653}
]

[
{"URI":"CoGroup/2","level":"total","name":null,"code":null,"baseColor":null,"secondaryColor":null,"selected":null,"value":317978,"Byyear":[],"count":10499},
{"URI":"intgroup/78","level":"total","name":null,"code":null,"baseColor":null,"secondaryColor":null,"selected":null,"value":105304,"Byyear":[],"count":1653},
{"URI":"integer/7005","level":"total","name":null,"code":null,"baseColor":null,"secondaryColor":null,"selected":null,"value":26857,"Byyear":[],"count":4542}
]
我曾尝试使用 Jackson 来比较使用 mapper.readtree 的响应,但结果返回为 false。
ObjectMapper mapper1 = new ObjectMapper();
ObjectMapper mapper2 = new ObjectMapper();
try{
assertEquals(mapper1.readTree(respStr1), mapper2.readTree(respStr2));
}
catch(Exception e) {
System.out.println(e);
}

and

ObjectMapper mapper1 = new ObjectMapper();
JsonNode tree1 = mapper1.readTree(respStr1);
JsonNode tree2 = mapper1.readTree(respStr2);
System.out.println(tree1.equals(tree2));

关于如何在此处进行比较的任何建议......

最佳答案

您可以使用图书馆 JSONAssert由 scyscreamer 为此目的。
它会像这样工作:

// respStr1 and respStr2 are the two json in string
JSONAssert.assertEquals(respStr1, respStr2, JSONCompareMode.NON_EXTENSIBLE);

NON_EXTENSIBLE如果 json 仅按顺序不同,则模式将允许断言通过。
编辑: JSONCompare.compareJSON(respStr1, respStr2, CompareMode.NON_EXTENSIBLE).passed()如果比较通过,将返回。

关于java - 当响应顺序不同时如何比较两个 JSon 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66754310/

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