Json差异比较

json差异比较

如何比较两个json的差异

代码实现

  1. 导入依赖
xml 复制代码
 <dependency>
            <groupId>cn.xiaoandcai</groupId>
            <artifactId>json-diff</artifactId>
            <!-- 旧版本可能存在某些缺陷。版本请以maven仓库最版为准。 -->
            <version>4.1.3-RC1-RELEASE</version>
        </dependency>

        <!-- 选择json解析框架。fastjson, fastjson2,gson,jackson 之一 -->
        <dependency>
            <groupId>cn.xiaoandcai</groupId>
            <artifactId>json-diff-impl-fastjson</artifactId>
            <version>4.1.3-RC1-RELEASE</version>
        </dependency>
  1. 用例实现
java 复制代码
 @Test
    public void testJsonDiff() throws JSONException, JsonProcessingException {
        String json1 = "{\"name\":\"John\", \"age\":30, \"city\":\"New York\",\"phone\":\"13800000000\",\"dog\":{\"age\":4,\"name\":\"mojo\",\"weight\":\"21.5\"}}";
        String json2 = "{\"name\":\"John\", \"age\":35, \"city\":\"Chicago\",\"qq\":\"123456789\",\"dog\":{\"name\":\"mojo\",\"age\":\"2\",\"color\":\"red\"}}";
        JsonComparedOption jsonComparedOption = new JsonComparedOption().setIgnoreOrder(true);
        JsonCompareResult jsonCompareResult = new DefaultJsonDifference()
                .option(jsonComparedOption)
                .detectDiff(json1, json2);

        List<Defects> defectsList = jsonCompareResult.getDefectsList();
        for (Defects d : defectsList) {
            Object actual = d.getActual();
            Object expect = d.getExpect();
            System.out.println("路径:" + d.getTravelPath().getAbstractTravelPath() + ",旧值:" + expect + ",新值:" + actual);
        }
    }

执行结果

相关推荐
nbsaas-boot2 天前
存储过程优化实践:统一返回结构、参数 JSON 化与事务原子化
数据库·json
思逻辑维4 天前
强大到工业层面的软件
数据结构·sql·sqlite·json
旺代4 天前
Qt中json的使用
qt·json
VB.Net4 天前
10.6.4 Json文件操作
c#·json
我码玄黄6 天前
利用JSON数据类型优化关系型数据库设计
数据库·后端·json
BYh_blog6 天前
基于nodejs+json+websocket+html的聊天应用
websocket·html·json
IT闫7 天前
【SpringMVC】——Json数据交互处理
json
请为小H留灯8 天前
Python读写各类数据文件
开发语言·python·jupyter·sqlite·json
周山至水数翠峰9 天前
.net 如何处理网页的Json请求?
服务器·json·.net
步、步、为营9 天前
C# 与.NET 日志变革:JSON 让程序“开口说清话”
c#·json·.net