当前位置: 首页 > news >正文

西安网站排名优化百度seo公司报价

西安网站排名优化,百度seo公司报价,网站建设与维护 东博,php企业公司网站源码简介 该方法用于比较两个Json文件是否完全一致,仅考虑内容若两个文件中的内容只是顺序不一致,内容是一样的,那么也代表这两个文件是相等的 实现代码 调用 using CompareJsonFiles;Console.WriteLine(" 输入信息 ");Console.WriteL…

简介

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

实现代码

  • 调用
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());
  • 实现
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;}
}
http://www.khdw.cn/news/51017.html

相关文章:

  • 源代码大全seo课程培训课程
  • 厦门市思明区建设局网站dw软件怎么制作网页
  • 电子信息工程专业招聘信息网杭州seo教程
  • 重庆建设安全管理网站百度推广优化排名怎么收费
  • 淘宝联盟自己做网站好的竞价推广托管
  • 西安网站建设公司广告投放方案
  • 怎么看网站是asp还是php免费产品推广网站
  • 打开建设银行网站初学seo网站推广需要怎么做
  • 户网站建设的不全.苏州网站开发公司
  • 阿里云怎么wordpressseo运营工作内容
  • 做多语言网站教程广告联盟平台排名
  • 网站建设网络公司培训班招生方案
  • 怎么给自己喜欢的人做网站郑州seo优化顾问
  • 专注Wordpress服务全网关键词优化公司哪家好
  • makah5页面制作网站百度导航下载2021最新版
  • 网络 网站谷歌官网
  • 网站制作的网站开发今日头条武汉最新消息
  • 英文案例网站2023年新冠疫情最新消息
  • 我要做网站推广企业网站页面设计
  • 开发公司注销了怎么办理房产证北京seo优化诊断
  • 广州本地做网站自建站怎么推广
  • 福州高级seo经理网站seo优化徐州百度网络
  • 黄骅做网站价格最好的bt种子搜索引擎
  • 电子商城市场aso优化是什么意思
  • wordpress 相册浏览seo人工智能
  • 网站搬家怎么做优化营商环境的措施建议
  • 河南最新今日头条保定百度seo公司
  • 帮别人做网站赚钱吗黄页推广2021
  • 网站建设与管理 教材网站宣传文案范例
  • 有哪些网站可以免费的关键信息基础设施安全保护条例