You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用C#比较两个JSON文件并找出差异对象

用C#识别两个JSON文件中的独有对象

要找出两个JSON数组中仅在其中一个文件里出现的对象,可按以下步骤实现:

1. 定义数据实体类(可选,若用动态JSON处理可跳过)

先创建与JSON结构匹配的实体类,方便后续序列化和比较:

public class DeliveryRecord
{
    public string SourceLocation { get; set; }
    public string DestinationLocation { get; set; }
    public string DeliveryDate { get; set; }

    // 重写相等判断方法,用于对象比较
    public override bool Equals(object obj)
    {
        if (obj is not DeliveryRecord other)
            return false;
        return SourceLocation == other.SourceLocation 
            && DestinationLocation == other.DestinationLocation 
            && DeliveryDate == other.DeliveryDate;
    }

    public override int GetHashCode()
    {
        return HashCode.Combine(SourceLocation, DestinationLocation, DeliveryDate);
    }
}

2. 读取并反序列化JSON文件

使用JSON库(推荐Newtonsoft.Json或System.Text.Json)读取文件内容并转成对象列表:

方式一:使用Newtonsoft.Json

using Newtonsoft.Json;
using System.Collections.Generic;
using System.IO;

// 读取两个JSON文件
string jsonContent1 = File.ReadAllText("json1.json");
string jsonContent2 = File.ReadAllText("json2.json");

// 反序列化为实体列表
List<DeliveryRecord> records1 = JsonConvert.DeserializeObject<List<DeliveryRecord>>(jsonContent1);
List<DeliveryRecord> records2 = JsonConvert.DeserializeObject<List<DeliveryRecord>>(jsonContent2);

方式二:使用System.Text.Json

using System.Text.Json;
using System.Collections.Generic;
using System.IO;

string jsonContent1 = File.ReadAllText("json1.json");
string jsonContent2 = File.ReadAllText("json2.json");

List<DeliveryRecord> records1 = JsonSerializer.Deserialize<List<DeliveryRecord>>(jsonContent1);
List<DeliveryRecord> records2 = JsonSerializer.Deserialize<List<DeliveryRecord>>(jsonContent2);

3. 筛选独有对象

利用LINQ的Except方法,结合自定义的相等判断逻辑,找出仅在单个列表中存在的对象:

找出仅在第二个JSON中存在的对象(对应示例期望结果)

var onlyInSecond = records2.Except(records1).ToList();

找出仅在第一个JSON中存在的对象

var onlyInFirst = records1.Except(records2).ToList();

找出两边所有的独有对象

var allUniqueRecords = onlyInFirst.Concat(onlyInSecond).ToList();

4. 序列化结果为JSON

将筛选出的独有对象转回JSON格式,可写入文件或直接输出:

用Newtonsoft.Json序列化

string resultJson = JsonConvert.SerializeObject(onlyInSecond, Formatting.Indented);
File.WriteAllText("unique_records.json", resultJson);

用System.Text.Json序列化

var options = new JsonSerializerOptions { WriteIndented = true };
string resultJson = JsonSerializer.Serialize(onlyInSecond, options);
File.WriteAllText("unique_records.json", resultJson);

无实体类的动态处理方案

如果不想定义实体类,可直接用JToken处理JSON:

using Newtonsoft.Json.Linq;
using System.IO;

JArray jArray1 = JArray.Parse(File.ReadAllText("json1.json"));
JArray jArray2 = JArray.Parse(File.ReadAllText("json2.json"));

// 找出仅在第二个JSON中的对象
var onlyInSecond = jArray2.Where(obj => !jArray1.Any(j => JToken.DeepEquals(j, obj))).ToList();

// 序列化结果
string resultJson = JsonConvert.SerializeObject(onlyInSecond, Formatting.Indented);
File.WriteAllText("unique_records.json", resultJson);

内容的提问来源于stack exchange,提问作者antabuz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 16:05:33