当前位置:首页 > 行业动态 > 正文

c#处理3种json数据的实例

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数组:

c#处理3种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"
  }
}

我们需要定义两个类,一个用于用户信息,另一个用于地址信息:

c#处理3种json数据的实例

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#类属性名称之间的映射关系。

c#处理3种json数据的实例

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值。