C# 语言 JSON序列化与反序列化

C#阿木 发布于 2025-06-13 7 次阅读


C 语言中的 JSON 序列化与反序列化技术详解

在当今的软件开发领域,JSON(JavaScript Object Notation)已经成为了一种非常流行的数据交换格式。它轻量级、易于阅读和编写,同时也易于机器解析和生成。C 作为一种强大的编程语言,提供了多种方式来进行 JSON 的序列化与反序列化。本文将围绕 C 语言中的 JSON 序列化与反序列化技术进行详细探讨。

JSON 序列化是将对象转换为 JSON 字符串的过程,而反序列化则是将 JSON 字符串转换回对象的过程。在 C 中,我们可以使用多种库来实现这一功能,包括 Newtonsoft.Json、System.Text.Json 和 Json.NET 等。本文将重点介绍这些库的使用方法。

Newtonsoft.Json 库

Newtonsoft.Json 是一个广泛使用的 JSON 库,它提供了丰富的功能和良好的性能。以下是如何使用 Newtonsoft.Json 库进行 JSON 序列化和反序列化的示例。

安装 Newtonsoft.Json 库

您需要在项目中添加 Newtonsoft.Json 库。可以通过 NuGet 包管理器进行安装:

shell
Install-Package Newtonsoft.Json

序列化对象

以下是一个使用 Newtonsoft.Json 库将 C 对象序列化为 JSON 字符串的示例:

csharp
using System;
using Newtonsoft.Json;

public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}

public class Program
{
public static void Main()
{
Person person = new Person { Name = "张三", Age = 30 };
string json = JsonConvert.SerializeObject(person);
Console.WriteLine(json);
}
}

反序列化 JSON 字符串

以下是如何将 JSON 字符串反序列化为 C 对象的示例:

csharp
using System;
using Newtonsoft.Json;

public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}

public class Program
{
public static void Main()
{
string json = "{"Name":"李四","Age":25}";
Person person = JsonConvert.DeserializeObject(json);
Console.WriteLine($"Name: {person.Name}, Age: {person.Age}");
}
}

System.Text.Json 库

System.Text.Json 是 .NET Core 3.0 及更高版本中引入的一个新的 JSON 库,它提供了高性能和低开销的 JSON 序列化和反序列化功能。

安装 System.Text.Json 库

由于 System.Text.Json 是 .NET Core 的一部分,因此不需要通过 NuGet 包管理器安装。只需确保您的项目支持 .NET Core 3.0 或更高版本即可。

序列化对象

以下是一个使用 System.Text.Json 库将 C 对象序列化为 JSON 字符串的示例:

csharp
using System;
using System.Text.Json;

public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}

public class Program
{
public static void Main()
{
Person person = new Person { Name = "王五", Age = 35 };
string json = JsonSerializer.Serialize(person);
Console.WriteLine(json);
}
}

反序列化 JSON 字符串

以下是如何将 JSON 字符串反序列化为 C 对象的示例:

csharp
using System;
using System.Text.Json;

public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}

public class Program
{
public static void Main()
{
string json = "{"Name":"赵六","Age":28}";
Person person = JsonSerializer.Deserialize(json);
Console.WriteLine($"Name: {person.Name}, Age: {person.Age}");
}
}

Json.NET 库

Json.NET 是一个由 James Newton-King 开发的 JSON 库,它提供了与 Newtonsoft.Json 库类似的功能。以下是如何使用 Json.NET 库进行 JSON 序列化和反序列化的示例。

安装 Json.NET 库

shell
Install-Package Json.NET

序列化对象

csharp
using System;
using Newtonsoft.Json.Linq;

public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}

public class Program
{
public static void Main()
{
Person person = new Person { Name = "孙七", Age = 40 };
string json = JToken.FromObject(person).ToString();
Console.WriteLine(json);
}
}

反序列化 JSON 字符串

csharp
using System;
using Newtonsoft.Json.Linq;

public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}

public class Program
{
public static void Main()
{
string json = "{"Name":"周八","Age":45}";
Person person = JToken.Parse(json).ToObject();
Console.WriteLine($"Name: {person.Name}, Age: {person.Age}");
}
}

总结

本文介绍了 C 语言中常用的 JSON 序列化与反序列化技术,包括 Newtonsoft.Json、System.Text.Json 和 Json.NET 库。这些库提供了丰富的功能和良好的性能,使得在 C 中处理 JSON 数据变得非常简单。选择合适的库取决于您的具体需求和项目环境。希望本文能帮助您更好地理解和应用这些技术。