2021-11-03

java  json比对



import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

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

/**
 * @author : hujun
 * @Description: demo
 * @date Date : 2021年11月01日 2:15 下午
 */

@Slf4j
public class JsonCompareUtil {

    public static String NONE = "不存在对应key";
    public static String SIZE = "数组长度不一致";
    public static String VALUE = "对应值不相等";
    public static String TYPE = "对应值类型不一致";
    public static List<String> exclude;
    public static List<String> keyList;

    public static JSONArray compareRun(JSONObject label, JSONObject result, String excludeString) {
        if (!StringUtils.isEmpty(excludeString)) {
            exclude = Arrays.asList(excludeString.split(","));
        }
        if (keyList == null) {
            keyList = new ArrayList<>();
            keyList.add("object");
        }
        JSONArray ret = compareJson(label, result, new JSONArray());
        keyList = null;
        exclude = null;
        return ret;
    }

    public static JSONArray compareJson(JSONObject label, JSONObject result, JSONArray error) {
        List<String> labelKey = new ArrayList<>();
        for (String s : label.keySet()) {
            labelKey.add(s);
            if (exclude != null && exclude.contains(s)) {
                continue;
            }
            keyList.add("." + s);
            if (result.get(s) == null && label.get(s) != null) {
                error.add(newItem(StringUtils.join(keyList, ""), "", label.getString(s), NONE));
                keyList.add("." + s);
                continue;
            }
//            if (result.get(key) == null && label.get(key) == null) {
//                return error.isEmpty() ? "" : error.toJSONString();
//            }
            compareJson(label.get(s), result.get(s), s, error);
            keyList.remove(keyList.size() - 1);
        }
        for (String s : result.keySet()) {
            if (!labelKey.contains(s)) {
                keyList.add("." + s);
                error.add(newItem(StringUtils.join(keyList, ""), result.getString(s), "", NONE));
                keyList.remove(keyList.size() - 1);
            }
        }
        return error;
    }

    public static void compareJson(Object label, Object result, String key, JSONArray error) {
        if (label instanceof JSONObject) {
            if (result instanceof JSONObject) {
                compareJson((JSONObject) label, (JSONObject) result, error);
            } else {
                error.add(newItem(StringUtils.join(keyList, ""), result.toString(), label.toString(), TYPE));
            }
        } else if (label instanceof JSONArray) {
            if (result instanceof JSONArray) {
                compareJson((JSONArray) label, (JSONArray) result, key, error);
            } else {
                error.add(newItem(StringUtils.join(keyList, ""), result.toString(), label.toString(), TYPE));
            }
        } else if (label instanceof String) {
            if (result instanceof String) {
                try {
                    String labelToStr = label.toString();
                    String resultToStr = result.toString();
                    compareJson(labelToStr, resultToStr, error);
                } catch (Exception e) {
                    error.add(newItem(StringUtils.join(keyList, ""), result.toString(), label.toString(), VALUE));
                }
            } else {
                error.add(newItem(StringUtils.join(keyList, ""), result.toString(), label.toString(), TYPE));
            }
        } else {
            compareJson(label.toString(), result.toString(), error);
        }
    }

    public static void compareJson(String labelToStr, String resultToStr, JSONArray error) {
        if (labelToStr != null && resultToStr != null) {
            if (!labelToStr.equals(resultToStr)) {
                error.add(newItem(StringUtils.join(keyList, ""), resultToStr, labelToStr, VALUE));
            }
        } else {
            error.add(newItem(StringUtils.join(keyList, ""), resultToStr, labelToStr, NONE));
        }
    }


    public static void compareJson(JSONArray label, JSONArray result, String key, JSONArray error) {
        if (label != null && result != null) {
            if (result.size() != label.size()) {
                error.add(newItem(StringUtils.join(keyList, ""), String.valueOf(result.size()), String.valueOf(label.size()), SIZE));
            }
            Iterator<Object> i1 = label.iterator();
            Iterator<Object> i2 = result.iterator();
            int count = 0;
            while (true) {
                if (i1.hasNext() && i2.hasNext()) {
                    keyList.add("[" + count + "]");
                    compareJson(i1.next(), i2.next(), key, error);
                    keyList.remove(keyList.size() - 1);
                } else if (i1.hasNext() && !i2.hasNext()) {
                    String value1 = i1.next().toString();
                    keyList.add("[" + count + "]");
                    error.add(newItem(StringUtils.join(keyList, ""), "", value1, NONE));
                    keyList.remove(keyList.size() - 1);
                } else if (!i1.hasNext() && i2.hasNext()) {
                    keyList.add("[" + count + "]");
                    String value1 = i2.next().toString();
                    error.add(newItem(StringUtils.join(keyList, ""), value1, "", NONE));
                    keyList.remove(keyList.size() - 1);
                } else {
                    break;
                }
                count += 1;
            }
        } else {
            if (label == null && result == null) {
                error.add(newItem(StringUtils.join(keyList, ""), "", "", NONE));
            } else if (label == null) {
                error.add(newItem(StringUtils.join(keyList, ""), result.toString(), "", NONE));
            } else if (result == null) {
                error.add(newItem(StringUtils.join(keyList, ""), "", label.toString(), NONE));
            } else {
                error.add(newItem(StringUtils.join(keyList, ""), result.toString(), label.toString(), NONE));
            }
        }
    }

    public static JSONObject newItem(String key, String result, String label, String reason) {
        JSONObject ret = new JSONObject();
        ret.put("key", key);
        ret.put("result", result);
        ret.put("label", label);
        ret.put("reason", reason);
        return ret;

    }

    public static void main(String[] args) throws Exception {
        JSONObject result = JSONObject.parseObject("{\"fast\":{\"link\":[{\"ces\":\"ces\"},{\"last\":\"las\"}],\"nGram\":214,\"totalCount\":735064,\"badCount\":1818,\"10+\":132},\"offline\":{\"3\":90,\"link\":[{\"ces\":\"ces\"},{\"last\":\"last\"}],\"nGram\":414,\"totalCount\":735063,\"badCount\":408,\"10+\":4}}");
//        JSONObject label = JSONObject.parseObject("{\"fast\":{\"3\":67,\"link\":[{\"ces\":\"ces\"},{\"last\":\"las\"}],\"nGram\":214,\"totalCount\":735064,\"badCount\":1818,\"10+\":132},\"offline\":{\"3\":90,\"link\":[{\"ces\":\"ces\"},{\"last\":\"last\"}],\"nGram\":414,\"totalCount\":735063,\"badCount\":408,\"10+\":4}}");
        JSONObject label = JSONObject.parseObject("{\"fast\":{\"3\":67,\"link\":[{\"ces\":\"ces\"},{\"last\":\"lsas\"}],\"totalCount\":735064,\"badCount\":1818,\"10+\":132},\"offline\":{\"3\":90,\"link\":[],\"nGram\":414,\"totalCount\":735063,\"badCount\":408,\"10+\":4}}");
        compareRun(label, result, "");
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值