背景
有兩個 Json 字符串闹蒜,格式都一樣艺玲,只是某些 value 值不同彤钟,現(xiàn)需要去除相同的部分羞秤,只保留不同的部分缸托;同時需要保持 Json 格式不變(層級不變)
工具
使用 Jackson
實現(xiàn)
public class KeepDiffJson {
public static void main(String[] args) throws IOException {
ObjectMapper mapper = new ObjectMapper();
// 第一個JSON字符串
String json1 = "{\n" +
" \"name\": \"John Doe\",\n" +
" \"age\": 30,\n" +
" \"city\": \"New York\",\n" +
" \"married\": true,\n" +
" \"children\": [\n" +
" {\n" +
" \"name\": \"Alice\",\n" +
" \"age\": 5\n" +
" },\n" +
" {\n" +
" \"name\": \"Bob\",\n" +
" \"age\": 8\n" +
" }\n" +
" ],\n" +
" \"pet\": {\n" +
" \"a\": {\n" +
" \"type\": \"dog\",\n" +
" \"name\": \"Rex\"\n" +
" },\n" +
" \"b\": {\n" +
" \"type\": \"cat\",\n" +
" \"name\": \"Whiskers\"\n" +
" }\n" +
" }\n" +
"}";
// 第二個JSON字符串
String json2 = "{\n" +
" \"name\": \"John Doe\",\n" +
" \"age\": 31,\n" +
" \"city\": \"New York\",\n" +
" \"married\": true,\n" +
" \"children\": [\n" +
" {\n" +
" \"name\": \"Alice...\",\n" +
" \"age\": 5\n" +
" },\n" +
" {\n" +
" \"name\": \"Bob\",\n" +
" \"age\": 8\n" +
" }\n" +
" ],\n" +
" \"pet\": {\n" +
" \"a\": {\n" +
" \"type\": \"dog1\",\n" +
" \"name\": \"Rex\"\n" +
" },\n" +
" \"b\": {\n" +
" \"type\": \"cat\",\n" +
" \"name\": \"Whiskers\"\n" +
" }\n" +
" }\n" +
"}";
// 將JSON字符串解析為JsonNode
JsonNode node1 = mapper.readTree(json1);
JsonNode node2 = mapper.readTree(json2);
// 對這兩個JsonNode進行比較并移除相同部分
removeCommonParts((ObjectNode) node1, (ObjectNode) node2);
// 將處理后的JsonNode轉(zhuǎn)換回JSON字符串
String resultJson1 = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(node1);
String resultJson2 = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(node2);
// System.out.println(resultJson1);
// System.out.println(resultJson2);
System.out.println(node1);
System.out.println(node2);
}
private static void removeCommonParts(ObjectNode json1, ObjectNode json2) {
Iterator<String> fieldNames = json1.fieldNames();
while (fieldNames.hasNext()) {
String fieldName = fieldNames.next();
JsonNode value1 = json1.get(fieldName);
JsonNode value2 = json2.get(fieldName);
if (value2 != null) {
if (value1.equals(value2)) {
fieldNames.remove();
json2.remove(fieldName);
} else if (value1.isObject() && value2.isObject()) {
removeCommonParts((ObjectNode) value1, (ObjectNode) value2);
} else if (value1.isArray() && value2.isArray()) {
removeCommonArrayElements((ArrayNode) value1, (ArrayNode) value2);
}
}
}
}
private static void removeCommonArrayElements(ArrayNode array1, ArrayNode array2) {
Iterator<JsonNode> iterator1 = array1.iterator();
Iterator<JsonNode> iterator2 = array2.iterator();
if (iterator1.hasNext() && !iterator2.hasNext()) {
throw new RuntimeException("Json 數(shù)據(jù)格式不匹配");
}
if (!iterator1.hasNext() && iterator2.hasNext()) {
throw new RuntimeException("Json 數(shù)據(jù)格式不匹配");
}
while (iterator1.hasNext()) {
JsonNode item1 = iterator1.next();
JsonNode item2 = iterator2.next();
System.out.println("item1: " + item1 + ", item2: " + item2);
// 方式 1:直接把數(shù)組中的 item 一次性對比
/*if (item1.equals(item2)) {
iterator1.remove();
iterator2.remove();
}*/
// 方式 2:遞歸對比數(shù)組中的 item
removeCommonParts((ObjectNode) item1, (ObjectNode) item2);
if (item1.isEmpty()) {
if (!item2.isEmpty()) {
throw new RuntimeException("Json 數(shù)據(jù)格式不匹配");
}
iterator1.remove();
iterator2.remove();
}
}
}
}
結(jié)果
輸入:
輸入.png
輸出:
輸出.png