Comparing two JSONs

我们两清 提交于 2019-12-21 20:57:10

问题


What's the best way to compare two JSON strings in Java? I want to be able to print out only the difference in key/values pairs.

I'm using gson library to convert it to a map and do assert but it displays both JSONs:

Type type = new TypeToken<Map<String, String>>() {}.getType();
Gson gson = new Gson();
Map<String, String> mapExpected = gson.fromJson(expectedJson, type);
Map<String, String> mapResponse = gson.fromJson(jsonResponse, type);
Assert.assertEquals(mapExpected, mapResponse);

Is there a better way to do this?


回答1:


It is tricky, but it can be done.

I would implement a Pair class that holds both String (key and value), with its corresponding equals() and hashcode();

Then put all elements from Map A as Pair in a Set (setA) and all elements from Map B in another Set (setB)

Then calculate

 Set<Pair> setA_B = setA.removeAll(setB);
 Set<Pair> setB_A = setB.removeAll(setA);
 Set<Pair> result = setA_B.addAll(setB_A);

In result there are only the elements that do not match. If all elements match (both original maps are equal) then result is empty.




回答2:


If you just wanted to compare simple equality, Jackson would make it easy; and just in case this might help it'd be:

JsonNode tree1 = objectMapper.readTree(json1);
JsonNode tree2 = objectMapper.readTree(json2);
if (!tree1.equals(tree2)) { // handle diffing
}

Now; since all JsonNode objects properly implement equality checks (so that ordering of keys does not matter etc), you could use this for recursive diffing to see where and how contents differ. For ObjectNodes you could get key names, remove same (tree1.getFieldNames().removeAll(tree2.getFieldNames()) and so on.




回答3:


Using SJuan76's solution, I was able to get just the difference in Key value pairs.

    Type type = new TypeToken<Map<String, String>>() {}.getType();
    Gson gson = new Gson();
    Map<String, String> mapExpected = gson.fromJson(expectedJson, type);
    Map<String, String> mapResponse = gson.fromJson(jsonResponse, type);
    Set<SimpleEntry<String,String>> expectedSet = new HashSet<SimpleEntry<String, String>>();
    Set<SimpleEntry<String, String>> tmpExpectedSet = new HashSet<SimpleEntry<String, String>>();
    Set<SimpleEntry<String, String>> responseSet = new HashSet<SimpleEntry<String, String>>();

    for (String key : mapExpected.keySet()) {
        expectedSet.add(new SimpleEntry<String, String>(key, mapExpected.get(key)));
        tmpExpectedSet.add(new SimpleEntry<String, String>(key, mapExpected.get(key)));
    }

    for (String key : mapResponse.keySet())
        responseSet.add((new SimpleEntry<String, String>(key, mapResponse.get(key))));

    expectedSet.removeAll(responseSet);
    responseSet.removeAll(tmpExpectedSet);
    expectedSet.addAll(responseSet);

    if (!expectedSet.isEmpty()) {
        for (SimpleEntry<String, String> diff : expectedSet)
            log.error(diff.getKey() + ":" + diff.getValue());
    }


来源:https://stackoverflow.com/questions/7128874/comparing-two-jsons

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!