鏡之Json Compare Diff

前言

“鏡” 寓意是凡事都有兩面性,Json對比也不例外!

因公司業(yè)務(wù)功能當(dāng)中有一個(gè)履歷的功能,它有多個(gè)版本的JSON數(shù)據(jù)需要對比出每個(gè)版本的不同差異節(jié)點(diǎn)并且將差異放置在一個(gè)新的JSON當(dāng)中原有結(jié)構(gòu)不能變動(dòng),差異節(jié)點(diǎn)使用數(shù)組對象的形式存儲(chǔ),前端點(diǎn)擊標(biāo)紅即可顯示多個(gè)版本的節(jié)點(diǎn)差異數(shù)據(jù)如下圖

banner
履歷查看

示例

// JSON One
{ 
    "employee":
    {
        "id": "1212",
        "fullName":"John Miles",
        "age": 34,
        "contact":
        {
            "email": "john@xyz.com",
            "phone": "9999999999"
        }
    }
}

// Json Two
{
    "employee":
    {
        "id": "1212",
        "ae86": "12162",
        "age": 34,
        "fullName": "John Miles111",
        "contact":
        {
            "email": "john@xyz.com",
            "phone": "我是改了的",
            "668": "999999991199"
        }
    }
}

可以看到 employee.ae86 是新增的。contact.668 也是新增的 phone 字段是修改了的

對比后的Json

// 獲取差異的節(jié)點(diǎn) 使用數(shù)組對象表示
{
    "employee/fullName/": [{
        "old": "John Miles"
    }, {
        "new": "John Miles111"
    }],
    "employee/contact/phone/": [{
        "old": "9999999999"
    }, {
        "new": "我是改了的"
    }],
    "employee/contact/668": [{
        "new": "999999991199"
    }],
    "employee/ae86": [{
        "new": "12162"
    }]
}

// 將差異節(jié)點(diǎn)的數(shù)據(jù)覆蓋上去

{
  "employee" : {
    "id" : "1212",
    "fullName" : [ {
      "old" : "John Miles"
    }, {
      "new" : "John Miles111"
    } ],
    "age" : 34,
    "contact" : {
      "email" : "john@xyz.com",
      "phone" : [ {
        "old" : "9999999999"
      }, {
        "new" : "我是改了的"
      } ],
      "668" : [ {
        "new" : "999999991199"
      } ]
    },
    "ae86" : [ {
      "new" : "12162"
    } ]
  }
}

實(shí)現(xiàn)

一、得到差異點(diǎn)Map

package com.yby6;


import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.JsonNodeType;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.apache.commons.lang3.ObjectUtils;

import java.io.IOException;
import java.util.*;

/**
 * @author Yang Shuai
 * Create By 2023/8/26
 */

public class JsonComparerUtils2 {
    static ObjectMapper mapper = new ObjectMapper();

    public static void main(String[] args) {

        String s1 = "{ \n" +
                "    \"employee\":\n" +
                "    {\n" +
                "        \"id\": \"1212\",\n" +
                "        \"fullName\":\"John Miles\",\n" +
                "        \"age\": 34,\n" +
                "        \"contact\":\n" +
                "        {\n" +
                "            \"email\": \"john@xyz.com\",\n" +
                "            \"phone\": \"9999999999\"\n" +
                "        }\n" +
                "    }\n" +
                "}";
        String s2 = "{\n" +
                "    \"employee\":\n" +
                "    {\n" +
                "        \"id\": \"1212\",\n" +
                "        \"ae86\": \"12162\",\n" +
                "        \"age\": 34,\n" +
                "        \"fullName\": \"John Miles111\",\n" +
                "        \"contact\":\n" +
                "        {\n" +
                "            \"email\": \"john@xyz.com\",\n" +
                "            \"phone\": \"我是改了的\",\n" +
                "            \"668\": \"999999991199\"\n" +
                "        }\n" +
                "    }\n" +
                "}";
        try {

            // 將json轉(zhuǎn)Json節(jié)點(diǎn)樹
            JsonNode node1 = mapper.readTree(s1);
            JsonNode node2 = mapper.readTree(s2);

            List<String> ignoreKey = new ArrayList<>();

            // 獲取兩個(gè)JSON之間的差異
            Map<String, Object> nodesDiff = getNodesDiff(node1, node2,
                    "", ignoreKey);
            System.out.println(mapper.writeValueAsString(nodesDiff));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }


    /**
     * 得到節(jié)點(diǎn)差異
     *
     * @param node1     node1
     * @param node2     node2
     * @param path      路徑
     * @param ignoreKey 忽略關(guān)鍵
     * @return {@link Map}<{@link String}, {@link Object}>
     */
    private static Map<String, Object> getNodesDiff(JsonNode node1, JsonNode node2, String path, List<String> ignoreKey) {
        Map<String, Object> diff = new LinkedHashMap<>();

        String[] split = path.split("/");
        String filed = split[split.length - 1];


        if (!node1.getNodeType().equals(node2.getNodeType())) {
            addToMap(path, node1, node2, diff, "update");
        } else {
            switch (node1.getNodeType()) {
                case OBJECT:

                    if (node1.isObject() && !node1.isEmpty()) {
                        for (Iterator<String> it = node1.fieldNames(); it.hasNext(); ) {
                            String fieldName = it.next();

                            JsonNode childNode1 = node1.get(fieldName);
                            JsonNode childNode2 = node2.get(fieldName);

                            // 忽略指定字段不對比
                            if (ignoreKey.contains(fieldName)) {
                                continue;
                            }

                            if (childNode2 != null) {
                                Map<String, Object> nestedDiff = getNodesDiff(childNode1, childNode2, path + fieldName + "/", ignoreKey);
                                if (!nestedDiff.isEmpty()) {
                                    diff.putAll(nestedDiff);
                                }
                            } else {
                                // 舊的存在新的則不存在表示刪除
                                addToMap(path + fieldName, childNode1, childNode1, diff, "delete");
                            }
                        }

                        for (Iterator<String> it = node2.fieldNames(); it.hasNext(); ) {
                            String fieldName = it.next();
                            if (ignoreKey.contains(fieldName)) {
                                continue;
                            }
                            // 如果舊的沒有這個(gè)數(shù)據(jù)那么表示新增
                            if (node1.get(fieldName) == null) {
                                addToMap(path + fieldName, null, node2.get(fieldName), diff, "add");
                            }
                        }
                    }


                    break;
                case ARRAY:
                    // 判斷兩個(gè)數(shù)組的長度不一樣則需要將兩個(gè)數(shù)組的長度補(bǔ)齊
                    if (node1.size() > 0 && node2.size() > 0 && node1.size() != node2.size()) {

                        try {
                            String m1 = mapper.writeValueAsString(node1);
                            String m2 = mapper.writeValueAsString(node2);

                            List list1 = mapper.readValue(m1, List.class);
                            List list2 = mapper.readValue(m2, List.class);


                            if (list1.size() > list2.size()) {

                                for (int i = list2.size(); i < list1.size(); i++) {
                                    String o = mapper.writeValueAsString(list1.get(i));
                                    JsonNode jsonNode = mapper.readTree(o);
                                    // 清空的
                                    clearNodeValues(jsonNode, ignoreKey);
                                    // 將jsonNode2添加到j(luò)sonNode1中
                                    ((ArrayNode) node2).add(jsonNode);
                                }
                            } else {
                                for (int i = list1.size(); i < list2.size(); i++) {
                                    String o = mapper.writeValueAsString(list2.get(i));
                                    JsonNode jsonNode = mapper.readTree(o);
                                    // 清空的
                                    clearNodeValues(jsonNode, ignoreKey);
                                    ((ArrayNode) node1).add(jsonNode);
                                }
                            }


                            // 排序數(shù)組

                            List<JsonNode> firstList = mapper.readValue(node1.traverse(), new TypeReference<List<JsonNode>>() {
                            });
                            List<JsonNode> secondList = mapper.readValue(node2.traverse(), new TypeReference<List<JsonNode>>() {
                            });


                            // 補(bǔ)齊后遞歸對比
                            for (int i = 0; i < firstList.size(); i++) {
                                Map<String, Object> nestedDiff = getNodesDiff(firstList.get(i), secondList.get(i), path + "[" + i + "]/", ignoreKey);
                                if (!nestedDiff.isEmpty()) {
                                    diff.putAll(nestedDiff);
                                }
                            }


                        } catch (IOException e) {
                            throw new RuntimeException(e);
                        }

                    } else {
                        // 判斷數(shù)組里面是不是對象
                        if (node1.size() > 0 && node1.get(0).getNodeType().equals(JsonNodeType.OBJECT)) {
                            for (int i = 0; i < node1.size(); i++) {
                                if (ignoreKey.contains(filed)) {
                                    break;
                                }
                                Map<String, Object> nestedDiff = getNodesDiff(node1.get(i), node2.get(i), path + "[" + i + "]/", ignoreKey);
                                if (!nestedDiff.isEmpty()) {
                                    diff.putAll(nestedDiff);
                                }
                            }
                        } else {
                            if (!node1.equals(node2)) {
                                if (ignoreKey.contains(filed)) {
                                    break;
                                }
                                addToMap(path, node1, node2, diff, "update");
                            }
                        }
                    }
                    break;
                case STRING:
                case BOOLEAN:
                case NUMBER:
                    if (ignoreKey.contains(filed)) {
                        break;
                    }

                    // 如果新的為空則為刪除
                    if (ObjectUtils.isEmpty(node2)) {
                        addToMap(path, node1, node2, diff, "delete");
                    }

                    if (!node1.equals(node2)) {
                        addToMap(path, node1, node2, diff, "update");
                    }

                    break;
                default:
                    throw new IllegalArgumentException("Unsupported JSON type:" + node1.getNodeType().name());
            }
        }

        return diff;
    }


    /**
     * 清空節(jié)點(diǎn)參數(shù)
     *
     * @param node 節(jié)點(diǎn)
     */
    private static void clearNodeValues(JsonNode node, List<String> ignoreKey) {
        // 忽略部分清空
        if (node.isObject()) {
            ObjectNode objectNode = (ObjectNode) node;
            objectNode.fields().forEachRemaining(entry -> {
                if (!ignoreKey.contains(entry.getKey())) {
                    objectNode.replace(entry.getKey(), null);
                }
            });
        } else if (node.isArray()) {
            for (JsonNode childNode : node) {
                clearNodeValues(childNode, ignoreKey);
            }
        }
    }


    /**
     * 將兩個(gè)json的差異添加到Map
     *
     * @param path     路徑
     * @param oldValue 舊值
     * @param newValue 新值
     * @param diff     diff
     */
    private static void addToMap(String path, JsonNode oldValue, JsonNode newValue, Map<String, Object> diff, String diffType) {
        List<Object> values = new ArrayList<>();
        HashMap<String, Object> map = new HashMap<>();
        map.put("old", oldValue != null ? getContent(oldValue) : "");
        map.put("new", newValue != null ? getContent(newValue) : "");
        map.put("diffType", diffType);
        values.add(map);
        diff.put(path, values.toArray(new Object[0]));
    }

    /**
     * 獲取內(nèi)容
     *
     * @param node 節(jié)點(diǎn)
     * @return {@link Object}
     */
    private static Object getContent(JsonNode node) {
        if (node.isBoolean()) {
            return node.asBoolean();
        } else if (node.isNumber()) {
            return node.asInt();
        } else if (node.isTextual()) {
            return node.asText();
        } else if (node.isObject()) {
            Map<String, Object> obj = new LinkedHashMap<>();
            for (Iterator<String> it = node.fieldNames(); it.hasNext(); ) {
                String propName = it.next();
                obj.put(propName, getContent(node.get(propName)));
            }
            return obj;
        } else if (node.isArray()) {
            ArrayNode array = (ArrayNode) node;
            Object[] contents = new Object[array.size()];
            for (int i = 0; i < array.size(); i++) {
                contents[i] = getContent(array.get(i));
            }
            return contents;
        } else if (node.isNull()) {
            return null;
        } else {
            throw new UnsupportedOperationException("不支持的 JSON 類型:" + node.getNodeType().name());
        }
    }

測試輸出

測試
{
    "employee/fullName/": [{
        "new": "John Miles111",
        "old": "John Miles",
        "diffType": "update"
    }],
    "employee/contact/phone/": [{
        "new": "我是改了的",
        "old": "9999999999",
        "diffType": "update"
    }],
    "employee/contact/668": [{
        "new": "999999991199",
        "old": "",
        "diffType": "add"
    }],
    "employee/ae86": [{
        "new": "12162",
        "old": "",
        "diffType": "add"
    }]
}

獲取差異代碼講解

這段代碼是一個(gè)處理兩個(gè) JSON 節(jié)點(diǎn)之間差異的方法,以及一些輔助方法。下面我將解釋每個(gè)方法的作用和代碼邏輯:

getNodesDiff 方法

描述

該方法用于比較兩個(gè) JSON 節(jié)點(diǎn)(node1node2)之間的差異,包括子節(jié)點(diǎn)差異,并返回一個(gè)表示差異的 Map。

方法簽名

private static Map<String, Object> getNodesDiff(JsonNode node1, JsonNode node2, String path, List<String> ignoreKey)

代碼解釋

  • diff 是一個(gè)用于存儲(chǔ)差異的 LinkedHashMap。
  • 首先,它根據(jù)路徑 path 中的最后一個(gè)部分(field)來確定節(jié)點(diǎn)的類型。
  • 然后,它檢查 node1node2 的節(jié)點(diǎn)類型是否相同,如果不同,將差異添加到 diff 中。
  • 如果節(jié)點(diǎn)類型相同,則根據(jù)節(jié)點(diǎn)類型進(jìn)行處理,包括對象、數(shù)組、字符串、布爾值和數(shù)字類型。
  • 對于對象類型,它遞歸地比較對象的字段,同時(shí)考慮了一些特殊情況,例如忽略指定的字段和 isValid 字段為 0 的情況。
  • 對于數(shù)組類型,它首先檢查數(shù)組長度是否不一致,如果不一致,則嘗試將兩個(gè)數(shù)組的長度補(bǔ)齊,然后遞歸比較數(shù)組元素。如果數(shù)組元素是對象類型,也會(huì)遞歸比較對象。
  • 對于其他基本數(shù)據(jù)類型,它會(huì)直接比較節(jié)點(diǎn)的值,如果不同,將差異添加到 diff 中。

clearNodeValues 方法

描述

這是一個(gè)輔助方法,用于清空節(jié)點(diǎn)的值,但保留節(jié)點(diǎn)結(jié)構(gòu)。

方法簽名

private static void clearNodeValues(JsonNode node, List<String> ignoreKey)

代碼解釋

  • 如果節(jié)點(diǎn)是對象類型,則清空對象中指定的字段,但忽略 ignoreKey 中的字段。
  • 如果節(jié)點(diǎn)是數(shù)組類型,則遞歸地清空數(shù)組元素的值,但保留數(shù)組結(jié)構(gòu)。

addToMap 方法

描述

這是一個(gè)輔助方法,用于將差異信息添加到差異 Map 中。

方法簽名

private static void addToMap(String path, JsonNode oldValue, JsonNode newValue, Map<String, Object> diff, String diffType)

代碼解釋

  • 該方法將差異信息以指定的格式添加到 diff 中,包括路徑 path、舊值 oldValue、新值 newValue 和差異類型 diffType

getContent 方法

描述

這是一個(gè)輔助方法,用于從 JsonNode 中提取內(nèi)容。

方法簽名

private static Object getContent(JsonNode node)

代碼解釋

  • 該方法根據(jù) JsonNode 的類型提取內(nèi)容,可能是布爾值、整數(shù)、字符串、對象、數(shù)組或 null 值。
  • 對于對象和數(shù)組類型,它遞歸提取內(nèi)容并返回。

二、合并

    /**
     * 將差異應(yīng)用到指定的 JSON 字符串,并返回處理后的字符串。
     *
     * @param json 要應(yīng)用差異的原始 JSON 字符串
     * @param diff 差異內(nèi)容,即 {@link #getNodesDiff} 返回的 Map 對象
     * @return 經(jīng)過差異處理后的 JSON 字符串
     */
    public static String applyDiff(String json, Map<String, Object> diff) throws IOException {
        JsonNode node = mapper.readTree(json);
        for (Map.Entry<String, Object> entry : diff.entrySet()) {
            String[] path = entry.getKey().split("/");
            JsonNode parentNode = node;
            for (int i = 0; i < path.length - 1; i++) {
                // 如果是null則跳過
                if (parentNode == null) {
                    continue;
                }
                // 如果該節(jié)點(diǎn)是數(shù)組那么解析一下
                if (parentNode.isArray()) {
                    int index = getIndexFromPath(path[i]);
                    parentNode = parentNode.get(index);
                } else {
                    parentNode = parentNode.get(path[i]);
                }
            }

            // 如果拿到的父節(jié)點(diǎn)是null則跳過
            if (parentNode == null) {
                continue;
            }

            String propertyName = path[path.length - 1];
            JsonNode childNode = parentNode.get(propertyName);


            if (entry.getValue() == null) {
                if (parentNode.isArray()) {
                    ((ArrayNode) parentNode).remove(Integer.parseInt(propertyName.substring(1, propertyName.length() - 1)));
                } else {
                    ((ObjectNode) parentNode).remove(propertyName);
                }
            } else {
                Object value = entry.getValue();
                // 是否是數(shù)組
                if (ArrayUtil.isArray(value)) {
//                    ArrayNode arrayNode = mapper.createArrayNode(); // 新建一個(gè)空的數(shù)組節(jié)點(diǎn)
//                    arrayNode.addPOJO(value);
                    ObjectMapper objectMapper = new ObjectMapper();
                    JsonNode arrayNode = objectMapper.valueToTree(value);
//                    Object[] arr = (Object[]) value;
//                    for (Object item : arr) {
//                        if (item != null) {
//                            // 將數(shù)組元素依次加入新建的數(shù)組節(jié)點(diǎn)中,不需要處理逗號(hào)問題
//                            arrayNode.addPOJO(item);
//                        }
//                    }
                    if (childNode != null && !childNode.isMissingNode()) { // 已經(jīng)存在該屬性,需要替換
                        ((ObjectNode) parentNode).replace(propertyName, arrayNode);
                    } else { // 不存在該屬性,直接應(yīng)用差異
                        // 如果父節(jié)點(diǎn)是數(shù)組,在數(shù)組末尾添加新元素
                        // 如果父節(jié)點(diǎn)是對象,在該對象中添加新屬性,值為空
                        if (parentNode.isArray()) {
                            int position = 0;
                            if (StringUtils.isNotBlank(propertyName)) {
                                position = Integer.parseInt(propertyName.substring(1, propertyName.length() - 1));
                            }

                            while (position > parentNode.size()) {
                                ((ArrayNode) parentNode).add(mapper.createObjectNode().put("", ""));
                            }
                            ((ArrayNode) parentNode).add(arrayNode);
                        } else {
                            ((ObjectNode) parentNode).set(propertyName, arrayNode);
                        }
                    }
                } else {
                    String newValue = entry.getValue().toString();
                    if (childNode == null || childNode.isNull() || childNode.isMissingNode()) {
                        if (parentNode.isArray()) { // 如果父節(jié)點(diǎn)是數(shù)組,在數(shù)組末尾添加新元素
                            ((ArrayNode) parentNode).add(mapper.createObjectNode().put(propertyName, ""));
                        } else { // 如果父節(jié)點(diǎn)是對象,在該對象中添加新屬性,值為空
                            ((ObjectNode) parentNode).put(propertyName, "");
                        }
                        childNode = parentNode.get(propertyName);
                    }
                    if (childNode.isValueNode()) {
                        if (childNode.isBoolean()) {
                            ((ObjectNode) parentNode).put(propertyName, Boolean.parseBoolean(newValue));
                        } else if (childNode.isIntegralNumber()) {
                            ((ObjectNode) parentNode).put(propertyName, newValue);
                        } else if (childNode.isFloatingPointNumber()) {
                            ((ObjectNode) parentNode).put(propertyName, Double.parseDouble(newValue));
                        } else if (childNode.isTextual()) {
                            try {
                                ((ObjectNode) parentNode).put(propertyName, newValue.substring(1, newValue.length() - 1)); // 去掉 JSON 字符串外層的雙引號(hào)
                            } catch (Exception e) {
                                ((ObjectNode) parentNode).put(propertyName, newValue);
                            }
                        }
                    } else {
                        ((ObjectNode) parentNode).set(propertyName, mapper.readTree(newValue));
                    }
                }
            }
        }
        return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(node);
    }


    /**
     * 移除方括號(hào)并將剩余字符串解析為整數(shù)索引
     *
     * @param path 路徑
     * @return int
     */
    private static int getIndexFromPath(String path) {
        return Integer.parseInt(path.substring(1, path.length() - 1));
    }

測試差異應(yīng)用

測試差異應(yīng)用
{
    "employee": {
        "id": "1212",
        "ae86": [{
            "new": "12162",
            "old": "",
            "diffType": "add"
        }],
        "age": 34,
        "fullName": [{
            "new": "John Miles111",
            "old": "John Miles",
            "diffType": "update"
        }],
        "contact": {
            "email": "john@xyz.com",
            "phone": [{
                "new": "我是改了的",
                "old": "9999999999",
                "diffType": "update"
            }],
            "668": [{
                "new": "999999991199",
                "old": "",
                "diffType": "add"
            }]
        }
    }
}

差異應(yīng)用代碼講解

applyDiff 方法

描述

該方法將差異應(yīng)用到指定的 JSON 字符串,并返回處理后的字符串。它接受一個(gè)原始的 JSON 字符串和一個(gè)差異的 Map,通常是從 getNodesDiff 方法獲取的。

方法簽名

public static String applyDiff(String json, Map<String, Object> diff) throws IOException

代碼解釋

  • 該方法首先使用 Jackson ObjectMapper mapper 將輸入的 JSON 字符串 json 解析為一個(gè) JsonNode 對象。
  • 遍歷差異的 Map 中的每個(gè)條目,每個(gè)條目表示要應(yīng)用到 JSON 的變更。
  • 對于每個(gè)條目,它通過 '/' 來分割條目的鍵(表示 JSON 內(nèi)的路徑),然后按照路徑迭代 JSON 結(jié)構(gòu),更新當(dāng)前節(jié)點(diǎn)指針。
  • 如果父節(jié)點(diǎn)為 null 或缺失,會(huì)跳過當(dāng)前迭代。
  • 根據(jù)條目的值是否為 null,它要么移除一個(gè)節(jié)點(diǎn),要么更新它:
- 如果值為 null,它會(huì)從 JSON 結(jié)構(gòu)中移除節(jié)點(diǎn)。如果父節(jié)點(diǎn)是數(shù)組,則移除指定索引處的元素;否則,從對象中移除指定屬性。
- 如果值不為 null,它會(huì)檢查值是否為數(shù)組。如果是數(shù)組,它會(huì)創(chuàng)建一個(gè)新的 JSON 數(shù)組節(jié)點(diǎn),并根據(jù)屬性是否已存在,要么替換要么添加到父節(jié)點(diǎn)中。如果值不是數(shù)組,則根據(jù)其類型(布爾值、數(shù)字、字符串或 JSON 對象)更新 JSON 結(jié)構(gòu)中的屬性。
  • 最后,它使用 mapper 將修改后的 JsonNode 轉(zhuǎn)換回 JSON 字符串,并返回結(jié)果的 JSON 字符串。

getIndexFromPath 方法

描述

這是一個(gè)私有的實(shí)用方法,用于移除字符串中的方括號(hào),并將剩余的字符串解析為整數(shù)索引。

方法簽名

private static int getIndexFromPath(String path)

代碼解釋

  • 該方法以一個(gè) path 字符串作為輸入。
  • 它移除 path 字符串的首尾字符(假設(shè)它們是方括號(hào)),然后將剩余的子串解析為整數(shù)索引。
  • 解析后的整數(shù)索引被返回。

over

最后

本期結(jié)束咱們下次再見??~

,關(guān)注我不迷路,如果本篇文章對你有所幫助,或者你有什么疑問,歡迎在評論區(qū)留言,我一般看到都會(huì)回復(fù)的。大家點(diǎn)贊支持一下喲~ ??

【選題思路】

基于兩串不同的JSON數(shù)據(jù)進(jìn)行對比出來差異再將差異應(yīng)用到最新的Json字符串當(dāng)中.

【寫作提綱】

一、前言

因公司業(yè)務(wù)功能當(dāng)中有一個(gè)履歷的功能,它有多個(gè)版本的JSON數(shù)據(jù)需要對比出每個(gè)版本的不同差異節(jié)點(diǎn)并且將差異放置在一個(gè)新的JSON當(dāng)中原有結(jié)構(gòu)不能變動(dòng),差異節(jié)點(diǎn)使用數(shù)組對象的形式存儲(chǔ),前端點(diǎn)擊標(biāo)紅即可顯示多個(gè)版本的節(jié)點(diǎn)差異數(shù)據(jù)

二、示例

介紹兩個(gè)Json的差異對比效果

三、實(shí)現(xiàn)

先得到兩個(gè)Json的差異節(jié)點(diǎn)集合、接著在最新的Json中轉(zhuǎn)換json節(jié)點(diǎn)對象進(jìn)行判斷每個(gè)節(jié)點(diǎn)的字段是否符合則插入到對應(yīng)的字段當(dāng)中!

本文由mdnice多平臺(tái)發(fā)布

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 一、簡介 json-diff是一款強(qiáng)大的,由java編寫的json差異發(fā)現(xiàn)工具。他可以發(fā)現(xiàn)任何結(jié)構(gòu)的json差異,...
    洋芋炒土豆絲閱讀 715評論 1 0
  • 一、摘要 今天推薦的是一款java中,對比兩個(gè)json-diff對象是否一致的工具包 json-diff[http...
    洋芋炒土豆絲閱讀 822評論 0 0
  • By Long Luo 引言 NOKIA 有句著名的廣告語:“科技以人為本”。任何技術(shù)都是為了滿足人的生產(chǎn)生活需要...
    Coder_LL閱讀 387評論 0 1
  • 前言 最近在開發(fā)過程中遇到了需要diff文件內(nèi)容或者大json的業(yè)務(wù)場景,發(fā)現(xiàn)了一個(gè)比較好用且經(jīng)典的js庫diff...
    廣蘭路地鐵閱讀 20,451評論 0 5
  • 1、各自定義 XML擴(kuò)展標(biāo)記語言 (Extensible Markup Language, XML) ,用于標(biāo)記電...
    插兜閱讀 705評論 0 2

友情鏈接更多精彩內(nèi)容