Java中多个Json字符串对比demo

这里使用得是阿里巴巴的fastjson包

public static void main(String[] args) {
        String a = "";
        String b = "";
        a = "[{\"mainTableNameCN\":\"主表名称1\",\"mainTableNameEN\":\"mainTableName\",\"mainTableField\":[{\"name\":\"businessNumber\",\"name_zh\":\"商机编号\",\"name_show\":\"商机编号\",\"type\":\"varchar\",\"description\":\"商机编号\",\"is_relation\":\"true\",\"relation_id\":\"code_321623414370\",\"item_type\":\"code\",\"LAY_TABLE_INDEX\":0},{\"name\":\"businessName\",\"name_zh\":\"商机名称\",\"name_show\":\"商机名称\",\"type\":\"varchar\",\"description\":\"商机名称\",\"is_relation\":\"true\",\"relation_id\":\"input_572045107844\",\"item_type\":\"input\",\"LAY_TABLE_INDEX\":1},{\"name\":\"judgeConvener\",\"name_zh\":\"评审召集人\",\"name_show\":\"评审召集人\",\"type\":\"varchar\",\"description\":\"评审召集人\",\"is_relation\":\"true\",\"relation_id\":\"input_624238012444\",\"item_type\":\"input\",\"LAY_TABLE_INDEX\":2},{\"name\":\"reviewTime\",\"name_zh\":\"评审时间\",\"name_show\":\"评审时间\",\"type\":\"varchar\",\"description\":\"评审时间\",\"is_relation\":\"true\",\"relation_id\":\"input_763487246487\",\"item_type\":\"input\",\"LAY_TABLE_INDEX\":3},{\"name\":\"reviewAddress\",\"name_zh\":\"评审地点\",\"name_show\":\"评审地点\",\"type\":\"varchar\",\"description\":\"评审地点\",\"is_relation\":\"true\",\"relation_id\":\"input_760308573244\",\"item_type\":\"input\",\"LAY_TABLE_INDEX\":4},{\"name\":\"reviewParticipation\",\"name_zh\":\"评审参加人员\",\"name_show\":\"评审参加人员\",\"type\":\"varchar\",\"description\":\"评审参加 人员\",\"is_relation\":\"true\",\"relation_id\":\"textarea_075567260317\",\"item_type\":\"textarea\",\"LAY_TABLE_INDEX\":5},{\"name\":\"mainOpinions\",\"name_zh\":\"主要意见\",\"name_show\":\"主要意见\",\"type\":\"varchar\",\"description\":\"主要意见\",\"is_relation\":\"true\",\"relation_id\":\"textarea_801121671777\",\"item_type\":\"textarea\",\"LAY_TABLE_INDEX\":6},{\"name\":\"reviewConclusion\",\"name_zh\":\"评审结论\",\"name_show\":\"评审结论\",\"type\":\"varchar\",\"description\":\"评审结论\",\"is_relation\":\"true\",\"relation_id\":\"checkbox_467861227571\",\"item_type\":\"checkBox\",\"LAY_TABLE_INDEX\":7},{\"name\":\"exceptionConclusion\",\"name_zh\":\"例外申请结论\",\"name_show\":\"例外申请结论\",\"type\":\"varchar\",\"description\":\"例外申请结论\",\"is_relation\":\"true\",\"relation_id\":\"textarea_827103384676\",\"item_type\":\"textarea\",\"LAY_TABLE_INDEX\":8},{\"name\":\"businesshead\",\"name_zh\":\"商机负责人\",\"name_show\":\"商机负责人\",\"type\":\"varchar\",\"description\":\"商机负责人\",\"is_relation\":false,\"relation_id\":\"input_141855644540\",\"item_type\":\"input\",\"LAY_TABLE_INDEX\":9}],\"subTableField\":[]}]";
        b = "[{\"mainTableNameCN\":\"主表名称\",\"mainTableNameEN\":\"mainTableName\",\"mainTableField\":[{\"name\":\"businessName\",\"name_zh\":\"商机名称\",\"name_show\":\"商机名称\",\"type\":\"varchar\",\"description\":\"商机名称\",\"is_relation\":\"true\",\"relation_id\":\"input_572045107844\",\"item_type\":\"input\",\"LAY_TABLE_INDEX\":1},{\"name\":\"judgeConvener\",\"name_zh\":\"评审召集人\",\"name_show\":\"评审召集人\",\"type\":\"varchar\",\"description\":\"评审召集人\",\"is_relation\":\"true\",\"relation_id\":\"input_624238012444\",\"item_type\":\"input\",\"LAY_TABLE_INDEX\":2},{\"name\":\"reviewTime\",\"name_zh\":\"评审时间\",\"name_show\":\"评审时间\",\"type\":\"varchar\",\"description\":\"评审时间\",\"is_relation\":\"true\",\"relation_id\":\"input_763487246487\",\"item_type\":\"input\",\"LAY_TABLE_INDEX\":3},{\"name\":\"reviewAddress\",\"name_zh\":\"评审地点\",\"name_show\":\"评审地点\",\"type\":\"varchar\",\"description\":\"评审地点\",\"is_relation\":\"true\",\"relation_id\":\"input_760308573244\",\"item_type\":\"input\",\"LAY_TABLE_INDEX\":4},{\"name\":\"reviewParticipation\",\"name_zh\":\"评审参加人员\",\"name_show\":\"评审参加人员\",\"type\":\"varchar\",\"description\":\"评审参加 人员\",\"is_relation\":\"true\",\"relation_id\":\"textarea_075567260317\",\"item_type\":\"textarea\",\"LAY_TABLE_INDEX\":5},{\"name\":\"mainOpinions\",\"name_zh\":\"主要意见\",\"name_show\":\"主要意见\",\"type\":\"varchar\",\"description\":\"主要意见\",\"is_relation\":\"true\",\"relation_id\":\"textarea_801121671777\",\"item_type\":\"textarea\",\"LAY_TABLE_INDEX\":6},{\"name\":\"reviewConclusion\",\"name_zh\":\"评审结论\",\"name_show\":\"评审结论\",\"type\":\"varchar\",\"description\":\"评审结论\",\"is_relation\":\"true\",\"relation_id\":\"checkbox_467861227571\",\"item_type\":\"checkBox\",\"LAY_TABLE_INDEX\":7},{\"name\":\"exceptionConclusion\",\"name_zh\":\"例外申请结论\",\"name_show\":\"例外申请结论\",\"type\":\"varchar\",\"description\":\"例外申请结论\",\"is_relation\":\"true\",\"relation_id\":\"textarea_827103384676\",\"item_type\":\"textarea\",\"LAY_TABLE_INDEX\":8},{\"name\":\"businesshead\",\"name_zh\":\"商机负责人\",\"name_show\":\"商机负责人\",\"type\":\"varchar\",\"description\":\"商机负责人\",\"is_relation\":false,\"relation_id\":\"input_141855644540\",\"item_type\":\"input\",\"LAY_TABLE_INDEX\":9},{\"name\":\"businessNumber\",\"name_zh\":\"商机编号\",\"name_show\":\"商机编号\",\"type\":\"varchar\",\"description\":\"商机编号\",\"is_relation\":\"true\",\"relation_id\":\"code_321623414370\",\"item_type\":\"code\",\"LAY_TABLE_INDEX\":0}],\"subTableField\":[]}]";
//        a = "[]";
        JSONArray js1 = JSONArray.parseArray(a);
        JSONArray js2 = JSONArray.parseArray(b);
//        Object js1 = JSONObject.fromObject(a);
//        Object js2 = JSONObject.fromObject(b);
        arrangeJson(js1);
        arrangeJson(js2);
        contrast(js1, js2);

        //net.sf.json包会认为 "field": abc123 非法,而com.google.gson包会自行转成 "field": "abc123"

//        JSONObject js3 = JSONObject.fromObject(a);
//        JSONObject js4 = JSONObject.fromObject(b);
//        arrangeJson(js3);
//        arrangeJson(js4);
//        contrast(js3, js4);
    }

    static void contrast(Object a, Object b) {
        boolean i = false;
        System.out.println("json对象a序列化=" + a.toString());
        System.out.println("json对象b序列化=" + b.toString());
        if (a.toString().equals(b.toString())) {
            i = true;
        }
        System.out.println("两个json对象是否等价:" + i);
    }

    //将json对象的各个元素重新排序
    static void arrangeJson(Object js) {
        if (js instanceof JSONObject) {
            JSONObject jsCopy = JSONObject.parseObject(js.toString());    //将json对象复制一份,进行递归遍历取值
            Iterator i = jsCopy.entrySet().iterator();
            ArrayList arr = new ArrayList();
            while (i.hasNext()) {
                Map.Entry entry = (Map.Entry) i.next();
                arr.add(entry.getKey().toString());
                //System.out.println(entry.getKey() + "    " + entry.getValue()+" "+jsCopy.get(entry.getKey()).getClass());
                ((JSONObject) js).remove(entry.getKey().toString());         //清空旧的子元素
            }
            sortArr(arr);
            for (int n = 0; n < arr.size(); n++) {
                //System.out.println("arr="+arr.get(n));
                String key = arr.get(n).toString();
                if (jsCopy.get(key) instanceof JSONObject || (jsCopy.get(key) instanceof JSONArray)) {
                    arrangeJson(jsCopy.get(key));         //递归调整json对象
                }
                ((JSONObject) js).put(key, jsCopy.get(key));               //重新组装序列化的子元素
            }
        }

        if (js instanceof JSONArray) {
            JSONArray jsCopy = JSONArray.parseArray(js.toString());
            ArrayList arr = new ArrayList();
            for (int n = 0; n < jsCopy.size(); n++) {
                arr.add(jsCopy.get(n));
                ((JSONArray) js).remove(0);
            }
            sortArr(arr);
            for (int n = 0; n < arr.size(); n++) {
                //System.out.println("arr_"+n+arr.get(n));
                arrangeJson((Object) arr.get(n));
                ((JSONArray) js).add((Object) arr.get(n));
            }
        }
    }

    //将数组元素按照哈希码从小到大重新排序
    private static void sortArr(ArrayList arr) {
        int len = arr.size();
        int[] n = new int[len];
        ArrayList arrCopy = (ArrayList) arr.clone();
        Object[] obj = new Object[len];
        for (int i = 0; i < len; i++) {
            n[i] = arrCopy.get(i).hashCode();
            obj[i] = arrCopy.get(i);
            arr.remove(0);
        }
        for (int i = 0; i < len; i++) {
            for (int y = i + 1; y < len; y++) {
                if (n[i] < n[y]) {
                    int x = n[y];
                    n[y] = n[i];
                    n[i] = x;
                    Object s = obj[y];
                    obj[y] = obj[i];
                    obj[i] = s;
                }
            }
        }
        for (int i = 0; i < len; i++) {
            arr.add(obj[i]);
        }
    }
 

                            
                        
                    
                    
                    

你可能感兴趣的:(Java中多个Json字符串对比demo)