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 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM