C#解析JSON 数据
2023-09-12 22:57:33
C# 处理 JSON 格式数据
前言
大家好,我是阿辉。
这几年在编程行业要是说哪个方向的语言发展迅速,那必须是属于前端。前端新的框架层出不穷,写法也很多。但是有一种是不变的,就是当需要和后端进行数据传输交换的时候,一直在使用 JSON。
JSON 是一种轻量级的数据交换格式,它基于 JavaScript 对象表示法。JSON 是文本格式的,因此很容易在网络上进行传输和解析。
C# 语言提供了丰富的库和工具,可以轻松地解析和使用 JSON 数据。在本文中,我将介绍如何使用 C# 解析 JSON 数据,并提供一些示例代码。
使用 Newtonsoft.Json 库解析 JSON 数据
Newtonsoft.Json 是一个流行的 C# 库,用于解析和生成 JSON 数据。该库提供了丰富的功能,包括:
- 支持多种 JSON 格式,包括标准 JSON、JSONP 和 BSON。
- 可以轻松地将 JSON 数据转换为 C# 对象。
- 可以轻松地将 C# 对象转换为 JSON 数据。
要使用 Newtonsoft.Json 库解析 JSON 数据,首先需要安装该库。可以通过 NuGet 包管理器安装 Newtonsoft.Json 库。
Install-Package Newtonsoft.Json
安装 Newtonsoft.Json 库后,就可以开始解析 JSON 数据了。下面是一个示例代码,演示如何使用 Newtonsoft.Json 库解析 JSON 数据:
string json = @"{
'name': 'John Doe',
'age': 30,
'occupation': 'Software Engineer'
}";
JObject jsonObject = JObject.Parse(json);
string name = (string)jsonObject["name"];
int age = (int)jsonObject["age"];
string occupation = (string)jsonObject["occupation"];
Console.WriteLine(string json = @"{
'name': 'John Doe',
'age': 30,
'occupation': 'Software Engineer'
}";
JObject jsonObject = JObject.Parse(json);
string name = (string)jsonObject["name"];
int age = (int)jsonObject["age"];
string occupation = (string)jsonObject["occupation"];
Console.WriteLine($"Name: {name}");
Console.WriteLine($"Age: {age}");
Console.WriteLine($"Occupation: {occupation}");
quot;Name: {name}");
Console.WriteLine(string json = @"{
'name': 'John Doe',
'age': 30,
'occupation': 'Software Engineer'
}";
JObject jsonObject = JObject.Parse(json);
string name = (string)jsonObject["name"];
int age = (int)jsonObject["age"];
string occupation = (string)jsonObject["occupation"];
Console.WriteLine($"Name: {name}");
Console.WriteLine($"Age: {age}");
Console.WriteLine($"Occupation: {occupation}");
quot;Age: {age}");
Console.WriteLine(string json = @"{
'name': 'John Doe',
'age': 30,
'occupation': 'Software Engineer'
}";
JObject jsonObject = JObject.Parse(json);
string name = (string)jsonObject["name"];
int age = (int)jsonObject["age"];
string occupation = (string)jsonObject["occupation"];
Console.WriteLine($"Name: {name}");
Console.WriteLine($"Age: {age}");
Console.WriteLine($"Occupation: {occupation}");
quot;Occupation: {occupation}");
这段代码首先定义了一个 JSON 字符串,然后使用 JObject.Parse() 方法将 JSON 字符串解析为 JObject 对象。
JObject 对象是一个动态对象,可以像字典一样访问其中的键值对。
这段代码使用索引器 [] 访问 JObject 对象中的键值对,并将其转换为相应的 C# 类型。
最后,这段代码将解析后的数据输出到控制台。
使用 DataContractJsonSerializer 类解析 JSON 数据
DataContractJsonSerializer 类是 .NET Framework 中的一个类,用于解析和生成 JSON 数据。该类提供了与 Newtonsoft.Json 库类似的功能,但使用起来更加简单。
要使用 DataContractJsonSerializer 类解析 JSON 数据,首先需要定义一个与 JSON 数据对应的 C# 类。
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public string Occupation { get; set; }
}
定义好 C# 类后,就可以使用 DataContractJsonSerializer 类解析 JSON 数据了。下面是一个示例代码,演示如何使用 DataContractJsonSerializer 类解析 JSON 数据:
string json = @"{
'name': 'John Doe',
'age': 30,
'occupation': 'Software Engineer'
}";
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Person));
using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json)))
{
Person person = (Person)serializer.ReadObject(ms);
Console.WriteLine(string json = @"{
'name': 'John Doe',
'age': 30,
'occupation': 'Software Engineer'
}";
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Person));
using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json)))
{
Person person = (Person)serializer.ReadObject(ms);
Console.WriteLine($"Name: {person.Name}");
Console.WriteLine($"Age: {person.Age}");
Console.WriteLine($"Occupation: {person.Occupation}");
}
quot;Name: {person.Name}");
Console.WriteLine(string json = @"{
'name': 'John Doe',
'age': 30,
'occupation': 'Software Engineer'
}";
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Person));
using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json)))
{
Person person = (Person)serializer.ReadObject(ms);
Console.WriteLine($"Name: {person.Name}");
Console.WriteLine($"Age: {person.Age}");
Console.WriteLine($"Occupation: {person.Occupation}");
}
quot;Age: {person.Age}");
Console.WriteLine(string json = @"{
'name': 'John Doe',
'age': 30,
'occupation': 'Software Engineer'
}";
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Person));
using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json)))
{
Person person = (Person)serializer.ReadObject(ms);
Console.WriteLine($"Name: {person.Name}");
Console.WriteLine($"Age: {person.Age}");
Console.WriteLine($"Occupation: {person.Occupation}");
}
quot;Occupation: {person.Occupation}");
}
这段代码首先定义了一个 JSON 字符串,然后使用 DataContractJsonSerializer 类创建一个新的序列化器。
接下来,这段代码使用 MemoryStream 类创建一个内存流,并将 JSON 字符串写入内存流中。
最后,这段代码使用 ReadObject() 方法从内存流中读取数据,并将其转换为 Person 对象。
Person 对象包含了从 JSON 数据中解析出的数据。这段代码将 Person 对象中的数据输出到控制台。
总结
C# 语言提供了丰富的库和工具,可以轻松地解析和使用 JSON 数据。
在本文中,我介绍了如何使用 Newtonsoft.Json 库和 DataContractJsonSerializer 类解析 JSON 数据。
这些库和类提供了丰富