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

java-json比对功能类

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

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.Arrays;
import java.util.Iterator;
import java.util.List;

@Slf4j
public class JsoncompairUtil {

    public static List exclude;

    public static boolean compareRun(JSonObject json1, JSonObject json2, String excludeString) {
        if (!StringUtils.isEmpty(excludeString)) {
            exclude = Arrays.asList(excludeString.split(","));
        }
        String ret = JsoncompairUtil.compareJson(json1, json2, null, new JSonObject());
        exclude = null;
        return ret.isEmpty();
    }

    public static String compareJson(JSonObject json1, JSonObject json2, String key, JSonObject error) {
        for (String s : json1.keySet()) {
            key = s;
            if (exclude != null && exclude.contains(key)) {
                continue;
            }
            if (json2.get(key) == null && json1.get(key) != null) {
                error.put("不存在的key:", error.containsKey("不存在的key:") ? error.getString("不存在的key:") + " ; " + key : key);
                continue;
            }
            if (json2.get(key) == null && json1.get(key) == null) {
                return error.isEmpty() ? "" : error.toJSonString();
            }
            compareJson(json1.get(key), json2.get(key), key, error);
        }
        return error.isEmpty() ? "" : error.toJSonString();
    }

    public static void compareJson(Object json1, Object json2, String key, JSonObject error) {
        if (json1 instanceof JSONObject) {
            if (json2 instanceof JSONObject) {
                compareJson((JSONObject) json1, (JSONObject) json2, key, error);
            } else {
                error.put("不一致的key:", error.containsKey("不一致的key:") ? error.getString("不一致的key:") + " ; " + key : key);
            }
        } else if (json1 instanceof JSONArray) {
            if (json2 instanceof JSONArray) {
                compareJson((JSONArray) json1, (JSONArray) json2, key, error);
            } else {
                error.put("不一致的key:", error.containsKey("不一致的key:") ? error.getString("不一致的key:") + " ; " + key : key);
            }

        } else if (json1 instanceof String) {
            if (json2 instanceof String) {
                try {
                    String json1ToStr = json1.toString();
                    String json2ToStr = json2.toString();
                    compareJson(json1ToStr, json2ToStr, key, error);
                } catch (Exception e) {
                    error.put("不一致的key:", error.containsKey("不一致的key:") ? error.getString("不一致的key:") + " ; " + key : key);
                }
            } else {
                error.put("不一致的key:", error.containsKey("不一致的key:") ? error.getString("不一致的key:") + " ; " + key : key);
            }
        } else {
            compareJson(json1.toString(), json2.toString(), key, error);
        }
    }

    public static void compareJson(String str1, String str2, String key, JSonObject error) {
        if (str1 != null && str2 != null) {
            if (!str1.equals(str2)) {
                error.put("不一致的key:", error.containsKey("不一致的key:") ? error.getString("不一致的key:") + " ; " + key : key);
            }
        } else {
            error.put("不存在的key:", error.containsKey("不存在的key:") ? error.getString("不存在的key:") + " ; " + key : key);
        }
    }

    public static void compareJson(JSonArray json1, JSonArray json2, String key, JSonObject error) {
        if (json1 != null && json2 != null) {
            Iterator i1 = json1.iterator();
            Iterator i2 = json2.iterator();
            while (i1.hasNext()) {
                compareJson(i1.next(), i2.next(), key, error);
            }
        } else {
            if (json1 == null && json2 == null) {
                error.put("不存在的key:", error.containsKey("不存在的key:") ? error.getString("不存在的key:") + " ; " + key : key);
            } else if (json1 == null) {
                error.put("不存在的key:", error.containsKey("不存在的key:") ? error.getString("不存在的key:") + " ; " + key : key);
            } else if (json2 == null) {
                error.put("不存在的key:", error.containsKey("不存在的key:") ? error.getString("不存在的key:") + " ; " + key : key);
            } else {
                error.put("不一致的key:", error.containsKey("不一致的key:") ? error.getString("不一致的key:") + " ; " + key : key);
            }
        }
    }


    public static void main(String[] args) throws Exception {
       

    }
}

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

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

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