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);
        }
    }

执行结果

相关推荐
芋头莎莎12 小时前
基于MQTT通讯UNIapp程序解析JSON数据
前端·uni-app·json
研☆香15 小时前
介绍一下JSON数据
json
电商API&Tina18 小时前
【电商API】淘宝/天猫拍立淘(按图搜索商品)API 全解析
大数据·开发语言·数据库·人工智能·json·图搜索算法
winfredzhang1 天前
实战复盘:如何用 HTML+JS+AI 打造一款“影迹”智能影视管理系统
javascript·html·json·加载·搜索·保存·电影接口
流沙QS2 天前
JSON序列化/反序列化工具改进版
java·json
曹牧2 天前
JSON文本格式化
json
ChindongX2 天前
garbage at the end of the document
qt·json
_xaboy2 天前
开源Vue组件-动态表单组件设计,告别重复CRUD,JSON一键生成表单
前端·vue.js·低代码·开源·json
一晌小贪欢2 天前
Python JSON 处理最佳实践:从入门到构建健壮的容错系统
开发语言·python·json·字典·python字典·python办公
yangminlei3 天前
Spring MVC 响应机制综合实践:页面、数据、JSON 与响应配置
spring·json·mvc