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, "");
}
}