csharp,using Newtonsoft.Json;,using System;class Program,{, static void Main(), {, string json1 = "{"Name":"John", "Age":30}";, var person = JsonConvert.DeserializeObject(json1);, Console.WriteLine($"Name: {person.Name}, Age: {person.Age}"); string json2 = "[{"Name":"Jane", "Age":25}, {"Name":"Doe", "Age":22}]";, var people = JsonConvert.DeserializeObject(json2);, foreach (var p in people), Console.WriteLine($"Name: {p.Name}, Age: {p.Age}"); string json3 = "{"Employees":[{"Name":"Alice", "Age":32}, {"Name":"Bob", "Age":40}]}";, var employees = JsonConvert.DeserializeObject(json3);, foreach (var e in employees.Employees), Console.WriteLine($"Name: {e.Name}, Age: {e.Age}");, },}public class Person,{, public string Name { get; set; }, public int Age { get; set; },}public class RootObject,{, public Person[] Employees { get; set; },},
“在C#中处理JSON数据通常使用Newtonsoft.Json
库,它提供了方便的方法来序列化和反序列化JSON,以下是处理三种不同类型JSON数据的实例:
简单对象
假设我们有以下简单的JSON数据,表示一个用户的信息:
{ "name": "John Doe", "age": 30, "email": "john.doe@example.com" }
我们需要定义一个与JSON结构相对应的类:
public class User { public string Name { get; set; } public int Age { get; set; } public string Email { get; set; } }
我们可以使用JsonConvert.DeserializeObject
方法将JSON字符串转换为User
对象:
string json = @"{ ""name"": ""John Doe"", ""age"": 30, ""email"": ""john.doe@example.com"" }"; User user = JsonConvert.DeserializeObject<User>(json); Console.WriteLine($"Name: {user.Name}, Age: {user.Age}, Email: {user.Email}");
数组/列表
考虑一个包含多个用户信息的JSON数组:
[ { "name": "John Doe", "age": 30, "email": "john.doe@example.com" }, { "name": "Jane Smith", "age": 25, "email": "jane.smith@example.com" } ]
我们同样定义一个User
类,然后使用JsonConvert.DeserializeObject
方法将JSON数组转换为User
对象的列表:
string jsonArray = @"[ { ""name"": ""John Doe"", ""age"": 30, ""email"": ""john.doe@example.com"" }, { ""name"": ""Jane Smith"", ""age"": 25, ""email"": ""jane.smith@example.com"" } ]"; List<User> users = JsonConvert.DeserializeObject<List<User>>(jsonArray); foreach (var user in users) { Console.WriteLine($"Name: {user.Name}, Age: {user.Age}, Email: {user.Email}"); }
嵌套对象
考虑一个更复杂的JSON结构,其中包含嵌套对象:
{ "user": { "name": "John Doe", "age": 30, "email": "john.doe@example.com" }, "address": { "street": "123 Main St", "city": "Anytown", "zipcode": "12345" } }
我们需要定义两个类,一个用于用户信息,另一个用于地址信息:
public class Address { public string Street { get; set; } public string City { get; set; } public string Zipcode { get; set; } } public class UserWithAddress { public User User { get; set; } public Address Address { get; set; } }
我们可以将JSON字符串反序列化为UserWithAddress
对象:
string nestedJson = @"{ ""user"": { ""name"": ""John Doe"", ""age"": 30, ""email"": ""john.doe@example.com"" }, ""address"": { ""street"": ""123 Main St"", ""city"": ""Anytown"", ""zipcode"": ""12345"" } }"; UserWithAddress userWithAddress = JsonConvert.DeserializeObject<UserWithAddress>(nestedJson); Console.WriteLine($"Name: {userWithAddress.User.Name}, Age: {userWithAddress.User.Age}, Email: {userWithAddress.User.Email}"); Console.WriteLine($"Street: {userWithAddress.Address.Street}, City: {userWithAddress.Address.City}, Zipcode: {userWithAddress.Address.Zipcode}");
相关问答FAQs
Q1: 如果JSON的属性名称与C#类的属性名称不匹配怎么办?
A1: 可以使用[JsonProperty]
属性来指定JSON属性名称与C#类属性名称之间的映射关系。
public class User { [JsonProperty("username")] public string Name { get; set; } }
这样,即使JSON中的键是"username"
,它也会被正确地映射到Name
属性上。
Q2: 如果JSON数据中的某些字段是可选的,如何处理?
A2: 可以将C#类中对应的属性设置为可空类型(如int?
、string?
等),或者在反序列化时使用JsonSerializerSettings
并设置NullValueHandling = NullValueHandling.Ignore
来忽略JSON中的null值。
原创文章,作者:未希,如若转载,请注明出处:https://www.kdun.com/ask/1656571.html
本网站发布或转载的文章及图片均来自网络,其原创性以及文中表达的观点和判断不代表本网站。如有问题,请联系客服处理。
发表回复