在C#中,可以使用Newtonsoft.Json库来处理多级JSON数据。
在C#中,处理多级JSON数据是一个常见的需求,本文将详细介绍如何在C#中解析和操作多级JSON数据,包括使用内置的System.Text.Json库以及第三方库如Newtonsoft.Json(也称为Json.NET)。
JSON解析与反序列化

使用System.Text.Json
System.Text.Json是.NET Core 3.0及以后版本中引入的用于处理JSON数据的内置库,它提供了高效且易于使用的API来解析和生成JSON数据。
1、安装与引用
如果使用的是.NET Core或.NET 5及以上版本,System.Text.Json已经包含在框架中,无需额外安装,对于早期版本的.NET Framework,可以通过NuGet包管理器安装:
Install-Package System.Text.Json
2、解析JSON
假设我们有以下多级JSON字符串:
{
"name": "John",
"age": 30,
"address": {
"street": "123 Main St",
"city": "New York",
"zipcode": "10001"
},
"phoneNumbers": [
{
"type": "home",
"number": "212-555-1234"
},
{
"type": "work",
"number": "646-555-5678"
}
]
}
我们可以使用以下代码将其解析为一个C#对象:
using System;
using System.Text.Json;
public class Address
{
public string Street { get; set; }
public string City { get; set; }
public string ZipCode { get; set; }
}
public class PhoneNumber
{
public string Type { get; set; }
public string Number { get; set; }
}
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public Address Address { get; set; }
public PhoneNumber[] PhoneNumbers { get; set; }
}
class Program
{
static void Main()
{
string jsonString = @"{
""name"": ""John"",
""age"": 30,
""address"": {
""street"": ""123 Main St"",
""city"": ""New York"",
""zipcode"": ""10001""
},
""phoneNumbers"": [
{
""type"": ""home"",
""number"": ""212-555-1234""
},
{
""type"": ""work"",
""number"": ""646-555-5678""
}
]
}";
Person person = JsonSerializer.Deserialize(jsonString);
Console.WriteLine($"Name: {person.Name}, Age: {person.Age}");
Console.WriteLine($"Address: {person.Address.Street}, {person.Address.City}, {person.Address.ZipCode}");
foreach (var phone in person.PhoneNumbers)
{
Console.WriteLine($"Phone: {phone.Type} {phone.Number}");
}
}
}
3、序列化回JSON
同样地,我们可以将C#对象序列化为JSON字符串:

string jsonOutput = JsonSerializer.Serialize(person, new JsonSerializerOptions { WriteIndented = true });
Console.WriteLine(jsonOutput);
使用Newtonsoft.Json(Json.NET)
Newtonsoft.Json是一个流行的第三方库,提供了丰富的功能来处理JSON数据,它支持更多的配置选项和高级特性。
1、安装与引用
通过NuGet包管理器安装:
Install-Package Newtonsoft.Json
2、解析JSON
使用Newtonsoft.Json解析上述相同的JSON字符串:
using System;
using Newtonsoft.Json;
public class Address
{
public string Street { get; set; }
public string City { get; set; }
public string ZipCode { get; set; }
}
public class PhoneNumber
{
public string Type { get; set; }
public string Number { get; set; }
}
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public Address Address { get; set; }
public PhoneNumber[] PhoneNumbers { get; set; }
}
class Program
{
static void Main()
{
string jsonString = @"{
""name"": ""John"",
""age"": 30,
""address"": {
""street"": ""123 Main St"",
""city"": ""New York"",
""zipcode"": ""10001""
},
""phoneNumbers"": [
{
""type"": ""home"",
""number"": ""212-555-1234""
},
{
""type"": ""work"",
""number"": ""646-555-5678""
}
]
}";
Person person = JsonConvert.DeserializeObject(jsonString);
Console.WriteLine($"Name: {person.Name}, Age: {person.Age}");
Console.WriteLine($"Address: {person.Address.Street}, {person.Address.City}, {person.Address.ZipCode}");
foreach (var phone in person.PhoneNumbers)
{
Console.WriteLine($"Phone: {phone.Type} {phone.Number}");
}
}
}
3、序列化回JSON
使用Newtonsoft.Json将对象序列化为JSON字符串:
string jsonOutput = JsonConvert.SerializeObject(person, Formatting.Indented); Console.WriteLine(jsonOutput);
JSON数据操作示例
以下是一些常见的JSON数据操作示例,包括添加、修改和删除节点。

添加新的节点
假设我们要在上述JSON数据中添加一个新的电话号码:
using System.Collections.Generic; using Newtonsoft.Json.Linq; // 如果使用Newtonsoft.Json // using System.Text.Json.Nodes; // 如果使用System.Text.Json JObject jsonObject = JObject.Parse(jsonString); // 如果使用Newtonsoft.Json // JsonNode jsonObject = JsonNode.Parse(jsonString); // 如果使用System.Text.Json
添加新的电话号码:
JArray phoneNumbers = (JArray)jsonObject["phoneNumbers"]; // 如果使用Newtonsoft.Json
// JsonNode phoneNumbers = jsonObject["phoneNumbers"]; // 如果使用System.Text.Json
phoneNumbers.Add(new JObject { { "type", "fax" }, { "number", "917-555-1234" } }); // 如果使用Newtonsoft.Json
// phoneNumbers["fax"] = new JObject { { "number", "917-555-1234" } }; // 如果使用System.Text.Json
修改现有节点
修改现有电话号码的类型:
phoneNumbers[0]["type"] = "mobile"; // 如果使用Newtonsoft.Json // phoneNumbers["home"]["type"] = "mobile"; // 如果使用System.Text.Json
删除节点
删除一个电话号码:
phoneNumbers.RemoveAt(1); // 如果使用Newtonsoft.Json
// phoneNumbers.Remove("work"); // 如果使用System.Text.Json
常见问题与解答栏目
Q1:System.Text.Json和Newtonsoft.Json有什么区别?我应该选择哪一个?
A1:System.Text.Json是.NET Core 3.0及更高版本中的内置库,性能较高且轻量级。Newtonsoft.Json则是一个功能强大的第三方库,提供了更多的配置选项和高级特性,如果你的项目已经在使用.NET Core或.NET 5及以上版本,并且对性能有较高要求,建议使用System.Text.Json,如果你需要更丰富的功能,或者已经在项目中使用了Newtonsoft.Json,可以继续使用它。
Q2: 如何解析嵌套的JSON数组?
A2: 解析嵌套的JSON数组与解析嵌套的对象类似,你可以将数组部分定义为相应的C#类型,
public class PhoneNumberList : List{}
然后在主类中使用这个类型:
public class Person
{
// ... other properties ...
public PhoneNumberList PhoneNumbers { get; set; }
}
到此,以上就是小编对于“c#多级json”的问题就介绍到这了,希望介绍的几点解答对大家有用,有任何问题和不懂的,欢迎各位朋友在评论区讨论,给我留言。