背景
有两个 Json 字符串,格式都一样,只是某些 value 值不同,现需要去除相同的部分,只保留不同的部分;同时需要保持 Json 格式不变(层级不变)
工具
使用 Jackson
实现
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转换回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 数据格式不匹配");
}
if (!iterator1.hasNext() && iterator2.hasNext()) {
throw new RuntimeException("Json 数据格式不匹配");
}
while (iterator1.hasNext()) {
JsonNode item1 = iterator1.next();
JsonNode item2 = iterator2.next();
System.out.println("item1: " + item1 + ", item2: " + item2);
// 方式 1:直接把数组中的 item 一次性对比
/*if (item1.equals(item2)) {
iterator1.remove();
iterator2.remove();
}*/
// 方式 2:递归对比数组中的 item
removeCommonParts((ObjectNode) item1, (ObjectNode) item2);
if (item1.isEmpty()) {
if (!item2.isEmpty()) {
throw new RuntimeException("Json 数据格式不匹配");
}
iterator1.remove();
iterator2.remove();
}
}
}
}
结果
输入:

输出:

网友评论