.Net_比对Json文件是否一致

简介

  • 该方法用于比较两个Json文件是否完全一致,仅考虑内容
  • 若两个文件中的内容只是顺序不一致,内容是一样的,那么也代表这两个文件是相等的

实现代码

  • 调用
csharp 复制代码
using CompareJsonFiles;

Console.WriteLine("================= 输入信息 ===================");

Console.WriteLine("请输入源Json文件路径:");
var sourcePath = Console.ReadLine();

Console.WriteLine($"{Environment.NewLine}请输入目标Json文件路径:");
var targetPath = Console.ReadLine();

Console.WriteLine($"{Environment.NewLine}您输入的源路径为:{sourcePath},目标路径为:{targetPath}");

Console.WriteLine($"================= 输入信息 =================== {Environment.NewLine}");


CompareJsonHelper.CompareJsonFiles(sourcePath?.Trim(), targetPath?.Trim());
  • 实现
csharp 复制代码
using Newtonsoft.Json.Linq;

namespace CompareJsonFiles;

/// <summary>
/// 比对Json文件的帮助类
/// </summary>
public static class CompareJsonHelper
{
    /// <summary>
    /// 比较两个JSON文件的内容
    /// </summary>
    /// <param name="sourcePath">源文件</param>
    /// <param name="targetPath">目标文件</param>
    public static void CompareJsonFiles(string? sourcePath, string? targetPath)
    {
        if (!IsExistForJsonFile(sourcePath, targetPath))
        {
            return;
        }
        
        // 加载并解析JSON文件
        var json1 = JToken.Parse(File.ReadAllText(sourcePath));
        var json2 = JToken.Parse(File.ReadAllText(targetPath));

        // 标准化JSON结构
        var normalizedSourceJson = NormalizeJson(json1);
        var normalizedTargetJson = NormalizeJson(json2);

        // 比较JSON内容
        var differences = FindDifferences(normalizedSourceJson, normalizedTargetJson, string.Empty);

        // 输出差异
        if (differences.Count == 0)
        {
            Console.WriteLine("======================== 两个文件内容一致!==============================");
        }
        else
        {
            Console.WriteLine("============================= 两个文件存在以下差异:=================================");
            foreach (var diff in differences)
            {
                Console.WriteLine(diff);
            }
        }
    }

    /// <summary>
    /// 监测源文件和目标文件是不是Json文件
    /// </summary>
    /// <param name="sourcePath"></param>
    /// <param name="targetPath"></param>
    /// <returns></returns>
    private static bool IsExistForJsonFile(string? sourcePath, string? targetPath)
    {
        if (string.IsNullOrWhiteSpace(sourcePath))
        {
            Console.WriteLine("错误信息:源文件路径不能为空");
            return false;
        }
        if (string.IsNullOrWhiteSpace(targetPath))
        {
            Console.WriteLine("错误信息:目标文件路径不能为空");
            return false;
        }
        if (!System.IO.File.Exists(sourcePath))
        {
            Console.WriteLine("错误信息:源文件不存在");
            return false;
        }
        if (!System.IO.File.Exists(targetPath))
        {
            Console.WriteLine("错误信息:目标文件不存在");
            return false;
        }
        if (Path.GetExtension(sourcePath) != ".json")
        {
            Console.WriteLine("错误信息:源文件不是Json文件");
            return false;
        }
        if (Path.GetExtension(targetPath) != ".json")
        {
            Console.WriteLine("错误信息:目标文件不是Json文件");
            return false;
        }

        return true;
    }
    
    /// <summary>
    /// 标准化JSON结构以便比较
    /// </summary>
    /// <param name="token"></param>
    /// <returns></returns>
    private static JToken NormalizeJson(JToken token)
    {
        switch (token.Type)
        {
            case JTokenType.Object:
                var sortedObject = new JObject();
                var properties = token.Children<JProperty>().ToList();
                properties.Sort((x, y) => string.Compare(x.Name, y.Name, StringComparison.Ordinal));
                foreach (var prop in properties)
                {
                    sortedObject.Add(prop.Name, NormalizeJson(prop.Value));
                }

                return sortedObject;
            case JTokenType.Array:
                var sortedArray = new JArray(token.Select(NormalizeJson).OrderBy(t => t.ToString()));
                return sortedArray;
            default:
                return token.DeepClone();
        }
    }

    /// <summary>
    /// 查找两个JSON之间的差异
    /// </summary>
    /// <param name="source"></param>
    /// <param name="target"></param>
    /// <param name="path"></param>
    /// <returns></returns>
    private static List<string> FindDifferences(JToken source, JToken target, string path)
    {
        var differences = new List<string>();

        if (source.Type != target.Type)
        {
            differences.Add($"路径 {path} 类型不同: {source.Type} 和 {target.Type}");
            return differences;
        }

        switch (source.Type)
        {
            case JTokenType.Object:
                foreach (var property in ((JObject)source).Properties())
                {
                    if (!((JObject)target).TryGetValue(property.Name, out JToken? tryTargetValue))
                    {
                        differences.Add($"路径 {path}.{property.Name} 在第二个文件中不存在");
                        continue;
                    }

                    differences.AddRange(FindDifferences(property.Value, tryTargetValue, $"{path}.{property.Name}"));
                }

                foreach (var property in ((JObject)target).Properties())
                {
                    if (!((JObject)source).TryGetValue(property.Name, out _))
                    {
                        differences.Add($"路径 {path}.{property.Name} 在第一个文件中不存在");
                    }
                }

                break;

            case JTokenType.Array:
                var arraySource = source as JArray;
                var arrayTarget = target as JArray;

                for (var i = 0; i < Math.Max(arraySource?.Count ?? 0, arrayTarget?.Count ?? 0); i++)
                {
                    if (i >= arraySource?.Count || i >= arrayTarget?.Count)
                    {
                        differences.Add($"路径 {path}[{i}] 数组长度不一致");
                        continue;
                    }

                    differences.AddRange(FindDifferences(arraySource![i], arrayTarget![i], $"{path}[{i}]"));
                }

                break;

            default:
                if (!JToken.DeepEquals(source, target))
                {
                    differences.Add($"路径 {path} 值不同: {source} 和 {target}");
                }

                break;
        }

        return differences;
    }
}
相关推荐
njsgcs13 分钟前
c# solidworks 折弯系数检查
开发语言·c#
格林威2 小时前
工业相机图像采集:Grab Timeout 设置建议——拒绝“假死”与“丢帧”的黄金法则
开发语言·人工智能·数码相机·计算机视觉·c#·机器视觉·工业相机
唐青枫2 小时前
C#.NET SignalR + Redis Backplane 深入解析:多节点部署与跨实例消息同步
c#·.net
Java开发追求者3 小时前
.NET Framework,Version=v4.8下载地址
.net·.net framework·version=v4.8
毕设源码-赖学姐3 小时前
【开题答辩全过程】以 基于.NET MVC的婚庆服务系统设计为例,包含答辩的问题和答案
mvc·.net
步步为营DotNet3 小时前
#.NET Aspire在云原生应用部署与管理中的深度实践
云原生·.net
ChoSeitaku4 小时前
NO.4|protobuf网络版通讯录|httplib|JSON、XML、ProtoBuf对比
xml·json
青衫客365 小时前
浅谈 Java 后端对象映射:从 JSON → VO → Entity 的原理与实践
java·json
qqxhb13 小时前
11|结构化输出:为什么 JSON 能让系统更稳定
json·ai编程·结构化·规范模板
FL162386312915 小时前
[C#][winform]segment-anything分割万物部署onnx模型一键抠图演示
开发语言·c#