packagecom.suncompass.huanjinyingji.uitl;importcom.alibaba.fastjson.JSON;importcom.alibaba.fastjson.JSONArray;importcom.alibaba.fastjson.JSONObject;importcom.alibaba.fastjson.serializer.SerializerFeature;import java.util.*;public classCompareJson {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";privateString itemKey;private List ignoreKeys = new ArrayList<>();publicCompareJson(String itemKey) {this.itemKey =itemKey;
}publicCompareJson(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, MapobjectMap) {
Iterator 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>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 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 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 findMap = null;for (Mapmap : 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, MapresultMap) {if (json1 instanceofJSONObject) {
Map objectMap = new HashMap<>();
compareJson((JSONObject) json1, (JSONObject) json2, objectMap);if (objectMap.size() > 0) {
resultMap.put(key, objectMap);
}
}else if (json1 instanceofJSONArray) {
JSONArray jsonArray=(JSONArray) json1;if (jsonArray != null && jsonArray.size() > 0) {if (!(jsonArray.get(0) instanceof JSONObject)) { //["1","2"],[1,2]...
Map compareMap = new HashMap<>();
compareMap.put(SysNew, json1);
compareMap.put(SysOld, json2);
resultMap.put(key, compareMap);return;
}
}
List> 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 compareMap = new HashMap<>();
compareMap.put(SysNew, json1);
compareMap.put(SysOld, json2);
resultMap.put(key, compareMap);
}
}publicString compareJson(String json1, String json2) {
Object jsonObj1=JSONObject.parse(json1);
Object jsonObj2=JSONObject.parse(json2);
Map resultMap = new HashMap<>();
compareJson(SysRoot, jsonObj1, jsonObj2, resultMap);
String resultStr= JSON.toJSONString(resultMap.get(SysRoot), newSerializerFeature[]{SerializerFeature.WriteMapNullValue});returnresultStr;
}
}