json 遞歸diff工具

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Random;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class CISCommon {
  
    
    /**
     * json diff工具
     * @param json1 
     * @param json2
     * @param key
     * @param skipKeys 不參與對比的key
     * @return
     */
    public String compareJson(JSONObject json1, JSONObject json2, String key,ArrayList<String> skipKeys) {
        StringBuffer sBuffer = new StringBuffer();
        String jsonString = "";
        Iterator<String> i = json1.keySet().iterator();
        while (i.hasNext()) {
            key = i.next();
            if(null!=skipKeys&&skipKeys.contains(key)) {
                continue;
            }
            //如果兩個joson裏都包含key,進入下一層比較
            if(json1.containsKey(key)&&json2.containsKey(key)) {
                jsonString = compareJson(json1.get(key), json2.get(key), key,skipKeys);
                sBuffer.append(jsonString);
            }else if(json1.containsKey(key)){
                sBuffer.append("josn2 不包含key:"+key);
            }else {
                sBuffer.append("josn1 不包含key:"+key);
            }
        }
        return sBuffer.toString();
    }

    public String compareJson(Object json1, Object json2, String key, ArrayList<String> skipKeys) {
        String jsonString = "";
        if (json1 instanceof JSONObject) {
            jsonString = compareJson((JSONObject) json1, (JSONObject) json2, key,skipKeys);
        } else if (json1 instanceof JSONArray) {
            jsonString = compareJson((JSONArray) json1, (JSONArray) json2, key,skipKeys);
        } else if (json1 instanceof String) {
            try {
                String json1ToStr = json1.toString();
                String json2ToStr = json2.toString();
                jsonString = compareJson(json1ToStr, json2ToStr, key,skipKeys);
            } catch (Exception e) {
                System.out.println("轉換髮生異常 key:" + key);
                e.printStackTrace();
            }
        } else {
            jsonString = compareJson(json1.toString(), json2.toString(), key,skipKeys);
        }
        return jsonString;
    }

    public String compareJson(String str1, String str2, String key,ArrayList<String> skipKeys) {
        StringBuffer sBuffer = new StringBuffer();
        if (!str1.equals(str2)) {
            sBuffer.append("不一致key【" + key + "】,json1:【" + str1 + "】,json2:【" + str2 +"】\n");
//            System.err.println("不一致key:" + key + ",json1:" + str1 + ",json2:" + str2);
        }
        return sBuffer.toString();
    }

    
    public String compareJson(JSONArray json1, JSONArray json2, String key,ArrayList<String> skipKeys) {
        StringBuffer sBuffer = new StringBuffer();
        if (json1 != null && json2 != null) {
            Iterator i1 = json1.iterator();
            Iterator i2 = json2.iterator();
            while (i1.hasNext()) {
                String diff= compareJson(i1.next(), i2.next(), key,skipKeys);
                sBuffer.append(diff);
            }
        } else {
            if (json1 == null && json2 == null) {
                sBuffer.append("不一致:key:" + key + "  在json1和json2中均不存在 \n");
//                System.err.println("不一致:key:" + key + "  在json1和json2中均不存在");
            } else if (json1 == null) {
                sBuffer.append("不一致:key:" + key + "  在json1中不存在 \n");
//                System.err.println("不一致:key:" + key + "  在json1中不存在");
            } else if (json2 == null) {
                sBuffer.append("不一致:key:" + key + "  在json2中不存在");
//                System.err.println("不一致:key:" + key + "  在json2中不存在");
            } else {
                sBuffer.append("不一致:key:" + key + "  未知原因");
//                System.err.println("不一致:key:" + key + "  未知原因");
            }
        }
        return sBuffer.toString();
    }
    
    
    
    private final static String st1 = "{\"username\":\"tom\",\"age\":18,\"jsona\":{\"tt\":\"http://wwwA.sohu.com\",\"qq:\":\"我的json串\",\"json2\":[{\"aa\":\"條件\",\"bb\":\"通過\"},{\"aa\":\"條件a\",\"bb\":\"通過baa\"}]}}";
    private final static String st2 = "{\"username\":\"tom\",\"age\":18,\"jsona\":{\"tt\":\"http://www.sohu.com\",\"qq:\":\"我的json串\",\"json2\":[{\"aa\":\"條件\",\"bb\":\"通過\"},{\"aa\":\"條件a\",\"bb\":\"通過b\"}]}}";
    public static void main(String[] args) {
        JSONObject jsonObject1 = JSONObject.parseObject(st1);
        JSONObject jsonObject2 = JSONObject.parseObject(st2);
        ArrayList<String> skipKeys = new ArrayList<String> (Arrays.asList("tt"));
        CISCommon ciscommon = new CISCommon();
        String diff = ciscommon.compareJson(jsonObject1, jsonObject2, null,skipKeys);
        System.out.println("diffInfo:"+diff);
    }
}
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章