栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

2021-11-03

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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;



@Slf4j
public class JsonCompareUtil {

    public static String NONE = "不存在对应key";
    public static String SIZE = "数组长度不一致";
    public static String VALUE = "对应值不相等";
    public static String TYPE = "对应值类型不一致";
    public static List exclude;
    public static List 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 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 i1 = label.iterator();
            Iterator 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, "");
}
}

 

转载请注明:文章转载自 www.mshxw.com
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号