java实现两个json的深度对比
两个json的深度对比
在网上找了好多资料都没有找到想要的,还是自己写个吧!
上代码!!!
1.pom.xml中加入
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.62</version>
</dependency>
2.新建CompareJson.java类
package com.suncompass.huanjinyingji.uitl; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature; import java.util.*; public class CompareJson { private static final String SysRoot = "sys_root";
private static final String SysType = "sys_type";
private static final String SysObj = "sys_obj";
private static final String SysNew = "sys_new";
private static final String SysOld = "sys_old";
private static final String TypeNew = "new";
private static final String TypeDelete = "delete";
private static final String TypeDifference = "difference"; private String itemKey;
private List<String> ignoreKeys = new ArrayList<>(); public CompareJson(String itemKey) {
this.itemKey = itemKey;
} public CompareJson(String itemKey, String ignoreKeys) {
this.itemKey = itemKey;
this.ignoreKeys = Arrays.asList(ignoreKeys.split("\\,"));
} public static void main(String[] args) {
final String json1 = "{\"id\":\"1\",\"name\":\"n1\",\"height\":null,\"list\":[1,2,3],\"age\":0,\"items\":[{\"id\":\"11\",\"name\":\"n11\",\"copyId\":\"1\"},{\"id\":\"12\",\"name\":\"n12\",\"copyId\":\"2\"}]}";
final String json2 = "{\"id\":\"1\",\"name\":\"n2\",\"height\":180,\"list\":[3,4,5],\"age\":null,\"items\":[{\"id\":\"11\",\"name\":\"n11\",\"copyId\":\"3\"},{\"id\":\"12\",\"name\":\"n13\",\"copyId\":\"2\"}]}";
String resultStr = new CompareJson("copyId").compareJson(json1, json2);
System.out.println(resultStr);
} private void compareJson(JSONObject jsonObject1, JSONObject jsonObject2, Map<String, Object> objectMap) {
Iterator<String> iterator = jsonObject1.keySet().iterator();
while (iterator.hasNext()) {
String key = iterator.next();
if (ignoreKeys.contains(key)) {
continue;
} Object value1 = jsonObject1.get(key);
Object value2 = jsonObject2.get(key);
compareJson(key, value1, value2, objectMap);
}
} private void compareJson(JSONArray jsonArray1, JSONArray jsonArray2, List<Map<String, Object>> arrayMap) { JSONArray jsonArray = (JSONArray) jsonArray1.clone();
if (jsonArray2 != null) {
jsonArray.addAll(jsonArray2);
} for (int i = 0; i < jsonArray.size(); i++) { JSONObject jsonObject = (JSONObject) jsonArray.get(i);
Object keyValue = jsonObject.get(this.itemKey);
if (keyValue == null) {
continue;
} JSONObject jsonObject1 = null;
for (int j = 0; j < jsonArray1.size(); j++) {
JSONObject jsonObj = (JSONObject) jsonArray1.get(j);
if (keyValue.equals(jsonObj.get(this.itemKey))) {
jsonObject1 = jsonObj;
break;
}
} JSONObject jsonObject2 = null;
for (int j = 0; j < jsonArray2.size(); j++) {
JSONObject jsonObj = (JSONObject) jsonArray2.get(j);
if (keyValue.equals(jsonObj.get(this.itemKey))) {
jsonObject2 = jsonObj;
break;
}
} Map<String, Object> objectMap = new HashMap<>();
if (jsonObject1 != null && jsonObject2 == null) {
objectMap.put(this.itemKey, keyValue);
objectMap.put(SysType, TypeNew);
objectMap.put(SysObj, jsonObject1);
} else if (jsonObject1 == null && jsonObject2 != null) {
objectMap.put(this.itemKey, keyValue);
objectMap.put(SysType, TypeDelete);
objectMap.put(SysObj, jsonObject2);
} else {
Map<String, Object> differenceMap = new HashMap<>();
compareJson(jsonObject1, jsonObject2, differenceMap);
if (differenceMap.size() > 0) {
objectMap.put(this.itemKey, keyValue);
objectMap.put(SysType, TypeDifference);
objectMap.put(SysObj, differenceMap);
}
} if (objectMap.size() > 0) { Map<String, Object> findMap = null;
for (Map<String, Object> map : arrayMap) {
if (keyValue.equals(map.get(this.itemKey))) {
findMap = map;
break;
}
} if (findMap == null) {
arrayMap.add(objectMap);
}
}
}
} private void compareJson(String key, Object json1, Object json2, Map<String, Object> resultMap) {
if (json1 instanceof JSONObject) { Map<String, Object> objectMap = new HashMap<>();
compareJson((JSONObject) json1, (JSONObject) json2, objectMap);
if (objectMap.size() > 0) {
resultMap.put(key, objectMap);
} } else if (json1 instanceof JSONArray) { JSONArray jsonArray = (JSONArray) json1;
if (jsonArray != null && jsonArray.size() > 0) {
if (!(jsonArray.get(0) instanceof JSONObject)) { //["1","2"],[1,2]...
Map<String, Object> compareMap = new HashMap<>();
compareMap.put(SysNew, json1);
compareMap.put(SysOld, json2);
resultMap.put(key, compareMap);
return;
}
} List<Map<String, Object>> arrayMap = new ArrayList<>();
compareJson((JSONArray) json1, (JSONArray) json2, arrayMap);
if (arrayMap.size() > 0) {
resultMap.put(key, arrayMap);
} } else if ((json1 == null && json2 != null) || (json1 != null && !json1.equals(json2))) {
Map<String, Object> compareMap = new HashMap<>();
compareMap.put(SysNew, json1);
compareMap.put(SysOld, json2);
resultMap.put(key, compareMap);
}
} public String compareJson(String json1, String json2) {
Object jsonObj1 = JSONObject.parse(json1);
Object jsonObj2 = JSONObject.parse(json2);
Map<String, Object> resultMap = new HashMap<>();
compareJson(SysRoot, jsonObj1, jsonObj2, resultMap);
String resultStr = JSON.toJSONString(resultMap.get(SysRoot), new SerializerFeature[]{SerializerFeature.WriteMapNullValue});
return resultStr;
}
}
3.运行main函数输出
{
"name": {
"sys_new": "n1",
"sys_old": "n2"
},
"list": {
"sys_new": [1, 2, 3],
"sys_old": [3, 4, 5]
},
"items": [{
"copyId": "1",
"sys_type": "new",
"sys_obj": {
"copyId": "1",
"name": "n11",
"id": "11"
}
}, {
"copyId": "2",
"sys_type": "difference",
"sys_obj": {
"name": {
"sys_new": "n12",
"sys_old": "n13"
}
}
}, {
"copyId": "3",
"sys_type": "delete",
"sys_obj": {
"copyId": "3",
"name": "n11",
"id": "11"
}
}],
"age": {
"sys_new": 0,
"sys_old": null
},
"height": {
"sys_new": null,
"sys_old": 180
}
}
总结:
1.支持深度;
2.支持集合指定标识设置(itemKey);
3.支持数组(如:[1,2,3]);
4.去除重复;
5.支持排除键设置(ignoreKeys);
java实现两个json的深度对比的更多相关文章
- 两款JSON类库Jackson与JSON-lib的性能对比(新增第三款测试)
本篇文章主要介绍了"两款JSON类库Jackson与JSON-lib的性能对比(新增第三款测试)",主要涉及到两款JSON类库Jackson与JSON-lib的性能对比(新增第三款 ...
- Java中的ReentrantLock和synchronized两种锁定机制的对比
问题:多个访问线程将需要写入到文件中的数据先保存到一个队列里面,然后由专门的 写出线程负责从队列中取出数据并写入到文件中. http://blog.csdn.net/top_code/article/ ...
- 比较任意两个JSON串是否相等(比较对象是否相等)JAVA版
废话少说,直接入题. 在面向对象语言中,经常会比较两个对象是否相等,而比较的大多是实体类实例,也就是封装数据的那些类实例,或者是Map.List互相嵌套成的复杂数据结构. 比较对象是否相等,常见的思路 ...
- Java构造和解析Json数据的两种方法详解二
在www.json.org上公布了很多JAVA下的json构造和解析工具,其中org.json和json-lib比较简单,两者使用上差不多但还是有些区别.下面接着介绍用org.json构造和解析Jso ...
- Java中两个List对比的算法
Java中两个List对比的算法: // 测试数据 // tdcsDdt.add("Z"); // tdcsDdt.add("B"); // tdcsDdt ...
- Java构造和解析Json数据的两种方法详解二——org.json
转自:http://www.cnblogs.com/lanxuezaipiao/archive/2013/05/24/3096437.html 在www.json.org上公布了很多JAVA下的jso ...
- Java构造和解析Json数据的两种方法详解一——json-lib
转自:http://www.cnblogs.com/lanxuezaipiao/archive/2013/05/23/3096001.html 在www.json.org上公布了很多JAVA下的jso ...
- Java中两种实现多线程方式的对比分析
本文转载自:http://www.linuxidc.com/Linux/2013-12/93690.htm#0-tsina-1-14812-397232819ff9a47a7b7e80a40613cf ...
- Splunk和ELK深度对比
转自:http://blog.51cto.com/splunkchina/1948105 日志处理两大生态Splunk和ELK深度对比 heijunmasd 0人评论 5312人阅读 2017-07- ...
随机推荐
- 代码审计-凡诺CMS 2.1文件包含漏洞
0x01代码审计 后台账号密码: admin admin 安装好了是这样的 漏洞文件:/channel.php if (ism()) { include($dir.$t_mpath.$c_mcmode ...
- LSTM神经网络走读
0设计概述 RNN梯度爆炸和消失比较严重,RNN隐层只有一个状态h记录短期记忆,增加一个长期记忆状态c似乎就可以解决问题.
- [Luogu2455] [SDOI2006]线性方程组
题目描述 已知n元线性一次方程组. 其中:n<=50, 系数是[b][color=red]整数<=100(有负数),bi的值都是整数且<300(有负数)(特别感谢U14968 mmq ...
- vue-router之to属性赋值
to属性赋值 <!-- html --> <div id="app"> <router-link to="/bj/朝阳区"> ...
- 利用hash远程登陆系统
有的时候当我们拿到系统管理员hash由于密码复杂度过高无法破解时候可以利用hash直接进行远程登录 我们用到Metasploit里面的模块 肉鸡为windwos server 2003 x32系统 1 ...
- .gitignore实现忽略提交
- Arduino学习笔记⑥ 硬件串口通信
1.前言 Ardunio与计算机通信最常用的方式就是串口通信.在Arduino控制器上,串口都是位于Rx和Tx两个引脚,Arduino的USB口通过一个转换芯片与这两个串口引脚连接.该转换芯片 ...
- 远程桌面连接(mstsc)
目录 1. 序言 2. 准备工作 3. 内网远程连接(以mstsc的方式) 4. 问题---凭据不工作 5. 外网远程连接(mstsc) 6. 结语 更新时间:2019.09.10 1. 序言 有时候 ...
- 网络数据请求request
关于网络数据请求的类很多,httpwebrequest,webrequest,webclient以及httpclient,具体差别在此不在赘述,在应用方面介绍webclient与httpclient则 ...
- springcloud之Feign、ribbon设置超时时间和重试机制的总结
一 超时时间配置 如果在一个微服务当中对同一个接口同时配置了Hystrix与ribbon两个超时时间,则在接口调用的时候,两个计时器会同时读秒. 比如,访问一个接口需要2秒,你的ribbon配置的超时 ...