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 {
}
}