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));
            }
        }
    }
}
相关推荐
汤姆yu10 分钟前
基于springboot的宠物服务管理系统
java·spring boot·后端
鹿角片ljp15 分钟前
智能家居控制系统Java实现
java·开发语言·智能家居
猿饵块27 分钟前
python--锁
java·jvm·python
Charlie_Byte40 分钟前
用 MurmurHash + Base62 生成短链接
java·后端
星辰落满衣41 分钟前
股票实时交易数据之Python、Java等多种主流语言实例代码演示通过股票数据接口
java·开发语言·python
利刃大大1 小时前
【SpringBoot】Spring IOC && DI && 五大注解 && Bean && 扫描路径 && 依赖注入
java·spring boot·spring
William_cl1 小时前
【CSDN 精品专栏】ASP.NET Razor 变量输出 @变量名:从入门到避坑,新手也能写对!
java·数据库·asp.net
尤物程序猿1 小时前
spring的监听器的几种使用方式
java·数据库·spring
老华带你飞1 小时前
学生请假管理|基于springboot 学生请假管理系统(源码+数据库+文档)
java·前端·数据库·vue.js·spring boot·后端·spring
毕设源码-钟学长1 小时前
【开题答辩全过程】以 基于java的点餐猫在线个性化点餐系统的设计与实现为例,包含答辩的问题和答案
java·开发语言