比较大量嵌套的Json并打印出结构和值上的差异

时间:2019-04-25 16:41:09

标签: java json comparison

我有一个严重嵌套的json,必须进行比较。比较是指结构(字段)相同且值相等。我还想打印出值和结构上的差异。是否有Java库可以帮助解决此问题?

2 个答案:

答案 0 :(得分:0)

我为电子游戏写了类似的东西。这是我使用的代码。它利用Ox

内的Json库
(select 1 as num)

答案 1 :(得分:0)

Gson库的实现:

public static void compareJson(JsonElement json, JsonElement other) {
    compareJson(json, other, "");
}

private static void compareJson(JsonElement json, JsonElement other, String path) {
    if (json.equals(other)) return;

    if (json.isJsonArray() && other.isJsonArray()) {
        JsonArray arrJ = (JsonArray) json;
        JsonArray arrO = (JsonArray) other;

        int size = Math.min(arrJ.size(), arrO.size());
        for (int i = 0; i < size; i++) {
            compareJson(arrJ.get(i), arrO.get(i), path + "/" + i);
        }
        if (arrJ.size() > arrO.size()) {
            for (int i = arrO.size(); i < arrJ.size(); i++) {
                System.out.println(path + "/ deleted array value: " + arrJ.get(i));
            }
        } else if (arrJ.size() < arrO.size()) {
            for (int i = arrJ.size(); i < arrO.size(); i++) {
                System.out.println(path + "/ added array value: " + arrO.get(i));
            }
        }
    } else if (json.isJsonObject() && other.isJsonObject()) {
        JsonObject objJ = (JsonObject) json;
        JsonObject objO = (JsonObject) other;

        for (Entry<String, JsonElement> entry : objJ.entrySet()) {
            String key = entry.getKey();
            JsonElement value = entry.getValue();
            if (objO.has(key)) {
                compareJson(value, objO.get(key), path + "/" + key);
            } else {
                System.out.println(path + "/ deleted object entry: \"" + key + "\": " + value);
            }
        }
        for (Entry<String, JsonElement> entry : objO.entrySet()) {
            String key = entry.getKey();
            JsonElement value = entry.getValue();
            if (!objJ.has(key)) {
                System.out.println(path + "/ added object entry: \"" + key + "\": " + value);
            }
        }
    } else if (json.isJsonPrimitive() && other.isJsonPrimitive()) {
        System.out.println(path + "/ value changed: " + json + " -> " + other);
    } else {
        System.out.println(path + "/ element changed: " + json + " -> " + other);
    }
}

使用:

JsonParser parser = new JsonParser();
compareJson(parser.parse(json1), parser.parse(json2));

示例输出:

/glossary/title/ value changed: "example glossary" -> "Example Glossary"
/glossary/GlossDiv/GlossList/GlossEntry/SortAs/ element changed: "SGML" -> null
/glossary/GlossDiv/GlossList/GlossEntry/GlossDef/GlossSeeAlso/0/ value changed: "GML" -> "XML"
/glossary/GlossDiv/GlossList/GlossEntry/GlossDef/GlossSeeAlso/ deleted array value: "XML"
/glossary/GlossDiv/GlossList/GlossEntry/ deleted object entry: "GlossSee": "markup"
/glossary/GlossDiv/GlossList/GlossEntry/ added object entry: "GlossType": "markup"