java 找出两个json文件的不同之处

java 复制代码
在这里插入代码片
package cn.test;

import com.google.gson.*;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Map;
import java.util.Set;

public class JsonDiff {

        public static void main(String[] args) throws IOException {



        String json1 = new String(Files.readAllBytes(Paths.get("/IdeaProjects/hello/src/main/java/cn/test/json1.txt")), StandardCharsets.UTF_8);
        String json2 = new String(Files.readAllBytes(Paths.get("/IdeaProjects/hello/src/main/java/cn/test/json2.txt")), StandardCharsets.UTF_8);

        diffJsonObjects(json1, json2);
    }

    public static void diffJsonObjects(String json1, String json2) {
        Gson gson = new Gson();
        JsonParser parser = new JsonParser();

        JsonElement element1 = parser.parse(json1);
        JsonElement element2 = parser.parse(json2);

        diffElements("root", element1, element2);
    }

    private static void diffElements(String path, JsonElement elem1, JsonElement elem2) {
        if (!elem1.equals(elem2)) {
            if (elem1.isJsonObject() && elem2.isJsonObject()) {
                diffJsonObjects(path, elem1.getAsJsonObject(), elem2.getAsJsonObject());
            } else if (elem1.isJsonArray() && elem2.isJsonArray()) {
                diffJsonArrays(path, elem1.getAsJsonArray(), elem2.getAsJsonArray());
            } else {
                System.out.println("Difference at path '" + path + "': " + elem1 + " != " + elem2);
            }
        }
    }

    private static void diffJsonObjects(String path, JsonObject obj1, JsonObject obj2) {
        Set<Map.Entry<String, JsonElement>> entries1 = obj1.entrySet();
        Set<Map.Entry<String, JsonElement>> entries2 = obj2.entrySet();

        for (Map.Entry<String, JsonElement> entry : entries1) {
            String key = entry.getKey();
            if (!obj2.has(key)) {
                System.out.println("Missing in second JSON at path '" + path + "." + key + "': " + entry.getValue());
            } else {
                diffElements(path + "." + key, entry.getValue(), obj2.get(key));
            }
        }

        for (Map.Entry<String, JsonElement> entry : entries2) {
            if (!obj1.has(entry.getKey())) {
                System.out.println("Missing in first JSON at path '" + path + "." + entry.getKey() + "': " + entry.getValue());
            }
        }
    }

    private static void diffJsonArrays(String path, JsonArray arr1, JsonArray arr2) {
        // 注意:这里简化处理,仅比较数组长度和按索引比较元素,实际应用中可能需要更复杂的逻辑处理不同长度或元素顺序变化的情况。
        if (arr1.size() != arr2.size()) {
            System.out.println("Array size difference at path '" + path + "': " + arr1.size() + " != " + arr2.size());
        } else {
            for (int i = 0; i < arr1.size(); i++) {
                diffElements(path + "[" + i + "]", arr1.get(i), arr2.get(i));
            }
        }
    }
}
相关推荐
Boilermaker19925 小时前
[Java 并发编程] Synchronized 锁升级
java·开发语言
Cherry的跨界思维5 小时前
28、AI测试环境搭建与全栈工具实战:从本地到云平台的完整指南
java·人工智能·vue3·ai测试·ai全栈·测试全栈·ai测试全栈
C_心欲无痕6 小时前
ts - tsconfig.json配置讲解
linux·前端·ubuntu·typescript·json
alonewolf_996 小时前
JDK17新特性全面解析:从语法革新到模块化革命
java·开发语言·jvm·jdk
一嘴一个橘子6 小时前
spring-aop 的 基础使用(啥是增强类、切点、切面)- 2
java
sheji34166 小时前
【开题答辩全过程】以 中医药文化科普系统为例,包含答辩的问题和答案
java
恋爱绝缘体17 小时前
2020重学C++重构你的C++知识体系
java·开发语言·c++·算法·junit
wszy18097 小时前
新文章标签:让用户一眼发现最新内容
java·python·harmonyos
wszy18097 小时前
顶部标题栏的设计与实现:让用户知道自己在哪
java·python·react native·harmonyos
程序员小假8 小时前
我们来说一下无锁队列 Disruptor 的原理
java·后端