Json对比工具类

1.新建对象,储存json不一致数据

/**
 * json数据不一致储存对象
 * 
 * @author deleba 2019年1月22日
 */
public class DifferentDataDTO {

    /**
     * 字段名
     */
    private String fieldName;
    /**
     * 新字段值
     */
    private String newFieldValue;
    /**
     * 旧字段值
     */
    private String oldFieldValue;

    public String getFieldName() {
        return fieldName;
    }

    public void setFieldName(String fieldName) {
        this.fieldName = fieldName;
    }

    public String getNewFieldValue() {
        return newFieldValue;
    }

    public void setNewFieldValue(String newFieldValue) {
        this.newFieldValue = newFieldValue;
    }

    public String getOldFieldValue() {
        return oldFieldValue;
    }

    public void setOldFieldValue(String oldFieldValue) {
        this.oldFieldValue = oldFieldValue;
    }

}

2.json比较工具类

  1 /**
  2  * 获取json不一致数据
  3  * 
  4  * @author deleba 2019年1月22日
  5  */
  6 public class JsonDifferentUtil {
  7 
  8     /**
  9      * jsonObejct 比较
 10      * 
 11      * @param newJson
 12      * @param oldJson
 13      * @param key
 14      * @return
 15      */
 16     public static List<DifferentDataDTO> compareJson(JSONObject newJson, JSONObject oldJson, String key) {
 17         List<DifferentDataDTO> list = new ArrayList<>();
 18         
 19         // 新旧数据都不为空
 20         if(newJson != null && oldJson != null ){
 21             Iterator<String> i = newJson.keySet().iterator();
 22             while (i.hasNext()) {
 23                 key = i.next();
 24                 // 获取Object 进行比较
 25                 list.addAll(compareJson(newJson.get(key), oldJson.get(key), key));
 26             }
 27         }else{// 新旧数据 有一个为空
 28             DifferentDataDTO differentDataDTO = new DifferentDataDTO();
 29             differentDataDTO.setFieldName(key);
 30 
 31             if (newJson == null || oldJson == null) {
 32                 differentDataDTO.setNewFieldValue(JSONObject.toJSONString(newJson));
 33                 differentDataDTO.setOldFieldValue(JSONObject.toJSONString(oldJson));
 34                 list.add(differentDataDTO);
 35             }
 36             // 其他情况不做处理
 37         }
 38         return list;
 39     }
 40 
 41     /**
 42      * Object 比较
 43      * 
 44      * @param newObj
 45      * @param oldObj
 46      * @param key
 47      * @return
 48      */
 49     public static List<DifferentDataDTO> compareJson(Object newObj, Object oldObj, String key) {
 50         List<DifferentDataDTO> list = new ArrayList<>();
 51         // 判断类型 JsonObject 则按照 JsonObject 进行比较
 52         if (newObj instanceof JSONObject) {
 53             list.addAll(compareJson((JSONObject) newObj, (JSONObject) oldObj, key));
 54             // 判断类型 JsonArray 则按照 JsonArray 进行比较
 55         } else if (newObj instanceof JSONArray) {
 56             list.addAll(compareJson((JSONArray) newObj, (JSONArray) oldObj, key));
 57             // 判断类型 String 则按照 String 进行比较
 58         } else if (newObj instanceof String) {
 59             String json1ToStr = newObj.toString();
 60             String json2ToStr = oldObj.toString();
 61             list.addAll(compareJson(json1ToStr, json2ToStr, key));
 62         } else {
 63             // 其他类型 按照 String 处理
 64             list.addAll(compareJson(newObj+"", oldObj+"", key));
 65         }
 66         return list;
 67     }
 68 
 69     /**
 70      * 字符串比较
 71      * 
 72      * @param newStr
 73      * @param oldStr
 74      * @param key
 75      * @return
 76      */
 77     public static List<DifferentDataDTO> compareJson(String newStr, String oldStr, String key) {
 78         List<DifferentDataDTO> list = new ArrayList<>();
 79         DifferentDataDTO differentDataDTO = new DifferentDataDTO();
 80         if (!newStr.equals(oldStr)) {
 81             differentDataDTO.setFieldName(key);
 82             differentDataDTO.setNewFieldValue(newStr);
 83             differentDataDTO.setOldFieldValue(oldStr);
 84             list.add(differentDataDTO);
 85         }
 86         // 一致,不做处理
 87         return list;
 88     }
 89 
 90     /**
 91      * JsonArray 比较
 92      * 
 93      * @param newJsonArray
 94      * @param oldjsonArray
 95      * @param key
 96      * @return
 97      */
 98     @SuppressWarnings("rawtypes")
 99     public static List<DifferentDataDTO> compareJson(JSONArray newJsonArray, JSONArray oldjsonArray, String key) {
100         List<DifferentDataDTO> list = new ArrayList<>();
101         // 新旧数据都不为空时,处理json数组内容
102         if (newJsonArray != null && oldjsonArray != null) {
103             Iterator i1 = newJsonArray.iterator();
104             Iterator i2 = oldjsonArray.iterator();
105             while (i1.hasNext()) {
106                 // 获取Object 进行比较
107                 list.addAll(compareJson(i1.next(), i2.next(), key));
108             }
109         } else {// 新旧数据 有一个为空
110             DifferentDataDTO differentDataDTO = new DifferentDataDTO();
111             differentDataDTO.setFieldName(key);
112 
113             if (newJsonArray == null || oldjsonArray == null) {
114                 differentDataDTO.setNewFieldValue(JSONObject.toJSONString(newJsonArray));
115                 differentDataDTO.setOldFieldValue(JSONObject.toJSONString(oldjsonArray));
116                 list.add(differentDataDTO);
117             }
118             // 其他情况不做处理
119         }
120         return list;
121     }
122 }

猜你喜欢

转载自www.cnblogs.com/deleba/p/10535057.html