public class JsonCompareUtil {
private static void printall(boolean issame, Stack<String> stack) {
if (issame) {
System.out.printf("相同:");
} else {
System.out.printf("不同:");
}
for (int i = 0; i < stack.size(); i++) {
String s = stack.get(i);
if (i == 0) {
System.out.printf("%s", s);
} else {
System.out.printf("->%s", s);
}
if (i == stack.size() - 1){
System.out.println();
}
}
}
private static void compareJson(JSONObject json1, JSONObject json2, Stack<String> stack) {
Set<String> stringSet1 = json1.keySet();
Set<String> stringSet2 = json2.keySet();
Set<String> keys = new HashSet<>();
keys.addAll(stringSet1);
keys.addAll(stringSet2);
for (String key : keys) {
stack.push(key);
Object value1 = json1.get(key);
Object value2 = json2.get(key);
if (value1 == null && value2 == null) {
printall(true, stack);
} else if (value1 instanceof JSONObject && value2 instanceof JSONObject) {
compareJson((JSONObject) value1, (JSONObject)value2, stack);
} else if (value1 instanceof JSONArray && value2 instanceof JSONArray) {
compareJson((JSONArray) value1, (JSONArray)value2, stack);
} else if (value1 instanceof String && value2 instanceof String) {
compareJson((String)value1, (String)value2, stack);
} else if (value1 == null || value2 == null) {
printall(false, stack);
}
stack.pop();
}
}
private static void compareJson(Object json1, Object json2, Stack<String> stack) {
if (json1 instanceof JSONObject) {
compareJson((JSONObject) json1, (JSONObject) json2, stack);
} else if (json1 instanceof JSONArray) {
compareJson((JSONArray) json1, (JSONArray) json2, stack);
} else if (json1 instanceof String) {
try {
String json1ToStr = json1.toString();
String json2ToStr = json2.toString();
compareJson(json1ToStr, json2ToStr, stack);
} catch (Exception e) {
System.out.println("转换发生异常 key:" + stack.peek());
e.printStackTrace();
}
} else {
compareJson(json1.toString(), json2.toString(), stack);
}
}
private static void compareJson(String str1, String str2, Stack<String> stack) {
printall(str1.equals(str2), stack);
}
private static void compareJson(JSONArray json1, JSONArray json2, Stack<String> stack) {
Object[] array1 = json1.toArray();
Object[] array2 = json2.toArray();
int length1 = array1.length;
int length2 = array2.length;
for (int i = 0; i < (length1>length2?length1:length2); i++) {
stack.push(String.valueOf(i));
if (i >= length1) {
printall(false, stack);
} else if (i >= length2) {
printall(false, stack);
} else {
compareJson(array1[i], array2[i], stack);
}
stack.pop();
}
}
public static void compareJson(String s1, String s2) {
Object o1 = JSON.parse(s1);
Object o2 = JSON.parse(s2);
compareJson(o1, o2, new Stack<String>());
}
public static void main(String[] args) {
compareJson("{'user':{'name':'wuliyu1', 'sex':'1'}, 'age':'18', 'birth':{'year':'1995', 'month':'11','day':'9'}, girlfriends:['aaa', 'bbb', 'ccc', 'eee', 'fff']}",
"{'user':{'name':'wuliyu2','six':'很6'}, 'age':'18', 'birth':{'year':'1995', 'month':'1','day':'9'}, girlfriends:['aaa', 'bbb', 'ccc', 'ddd', 'fff']}");
}
}