使用 Java 代码获取两个 JSON 字符串的差异

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3214108/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 17:29:15  来源:igfitidea点击:

Getting a diff of two JSON strings using Java code

javajson

提问by Prafull N

Can anybody suggest some Java Library or Code to get a diff of two JSON Strings?

有人可以建议一些 Java 库或代码来获取两个 JSON 字符串的差异吗?

回答by Justin Niessner

Personally, I would suggest de-serializing the JSON strings back into objects and comparing the objects.

就个人而言,我建议将 JSON 字符串反序列化回对象并比较对象。

That way you don't have to worry about extra whitespace/formatting between the two JSON strings (two strings could be formatted wildly different and still represent equal objects).

这样你就不必担心两个 JSON 字符串之间的额外空格/格式(两个字符串的格式可能大不相同,但仍然代表相同的对象)。

回答by StaxMan

For one specific suggestion, you could use Hymanson, bind JSON strings into JSON trees, and compare them for equality. Something like:

对于一个特定的建议,您可以使用Hymanson,将 JSON 字符串绑定到 JSON 树中,然后比较它们的相等性。就像是:

ObjectMapper mapper = new ObjectMapper();
JsonNode tree1 = mapper.readTree(jsonString1);
JsonNode tree2 = mapper.readTree(jsonString2);
if (tree1.equals(tree2)) { 
  // yes, contents are equal -- note, ordering of arrays matters, objects not
} else { 
  // not equal
}

equality comparison is by value and should work as expected with respect to JSON arrays, objects and primitive values.

相等比较是按值进行的,对于 JSON 数组、对象和原始值应该按预期工作。

回答by Martin Algesten

I had the exact same problem and ended up writing my own library:

我遇到了完全相同的问题,最终编写了自己的库:

https://github.com/algesten/jsondiff

https://github.com/algesten/jsondiff

It does both diffing/patching.

它同时进行差异/修补。

Diffs are JSON-objects themselves and have a simple syntax for object merge/replace and array insert/replace.

差异本身就是 JSON 对象,并且具有用于对象合并/替换和数组插入/替换的简单语法。

Example:

例子:

original
{
   a: { b: 42 }
}

patch
{
  "~a" { c: 43 }
}

The ~indicates an object merge.

~指示对象合并。

result
{
   a: { b: 42, c: 43 }
}

回答by Dungeon

The above answers only checks Json against equality.If you want to get a diff look at javers library. Convert the json into java objects using Hymanson and then use javers compare value objects to get the diff. Here is the sample piece code.(Assuming p1,p2 are two java objects)

上面的答案只检查 Json 是否相等。如果您想查看 javers 库的差异。使用 Hymanson 将 json 转换为 java 对象,然后使用 javers 比较值对象来获取差异。这是示例代码。(假设 p1,p2 是两个 java 对象)

Javers j = JaversBuilder.javers().build();
        Diff diff = j.compare(p1, p2);
        if (diff.hasChanges()) {
            List<Change> changes = diff.getChanges();
            for (Change change : changes) {
                if (change instanceof ValueChange) {
                    ValueChange valChange = (ValueChange) change;
System.out.println(valChange.getPropertyName() + " -- " + valChange.getLeft() + "--"+valchange.getRight());
}
 }

This will give you the differences as key value and the expected and actual values.

这将为您提供关键值以及预期值和实际值的差异。