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



