如何在 JSON.NET 中反序列化具有嵌套对象列表的对象列表?
How to deserialize in JSON.NET a list of objects with a nested list of objects?
我有 json 这样的文件。
[
{
"listMember": [
{
"Name": "Cris",
"Position": "Командир",
"Age": 50,
"Experience": 25
},
{
"Name": "Sandra",
"Position": "Стюардесса",
"Age": 25,
"Experience": 5
}
],
"Id": 31004,
"Type": "грузовой",
"Model": "Bell",
"Year_of_issue": 2007,
"Seats": 150,
"Carrying": 50,
"Maintenance": "12.04.2017"
}
]
飞机列表:
class Airplane
{
public List<Member> listMember = new List<Member>();
public Int16 Id { get; set; }
public String Type { get; set; }
public String Model { get; set; }
public Int16 Year_of_issue { get; set; }
public Int16 Seats { get; set; }
public Int16 Carrying { get; set; }
public String Maintenance { get; set; }
}
成员的嵌套列表。
class Member
{
public string Name { get; set; }
public string Position { get; set; }
public UInt16 Age { get; set; }
public UInt16 Experience { get; set; }
}
我不知道如何反序列化这个文件。
我试过这样做...
List<Airport> airport = JsonConvert.DeserializeObject<List<Airport>>(json);
但它不起作用。
Newtonsoft.Json.JsonSerializationException:无法将当前 JSON 对象(例如 {"name":"value"})反序列化为类型 'lab7.Airport',因为该类型需要 JSON 数组(例如 [1,2,3])以正确反序列化。
要修复此错误,请将 JSON 更改为 JSON 数组(例如 [1,2,3])或更改反序列化类型,使其成为普通的 .NET 类型(例如,不是原始类型像整数,而不是像数组或列表这样的集合类型),可以从 JSON 对象反序列化。 JsonObjectAttribute 也可以添加到类型以强制它从 JSON 对象反序列化。
在此先感谢您的帮助。
您可以通过此网站生成您的 类:JSON to C#
它导致此代码:
public class ListMember
{
public string Name { get; set; }
public string Position { get; set; }
public int Age { get; set; }
public int Experience { get; set; }
}
public class RootObject
{
public List<ListMember> listMember { get; set; }
public int Id { get; set; }
public string Type { get; set; }
public string Model { get; set; }
public int Year_of_issue { get; set; }
public int Seats { get; set; }
public int Carrying { get; set; }
public string Maintenance { get; set; }
}
我有 json 这样的文件。
[
{
"listMember": [
{
"Name": "Cris",
"Position": "Командир",
"Age": 50,
"Experience": 25
},
{
"Name": "Sandra",
"Position": "Стюардесса",
"Age": 25,
"Experience": 5
}
],
"Id": 31004,
"Type": "грузовой",
"Model": "Bell",
"Year_of_issue": 2007,
"Seats": 150,
"Carrying": 50,
"Maintenance": "12.04.2017"
}
]
飞机列表:
class Airplane
{
public List<Member> listMember = new List<Member>();
public Int16 Id { get; set; }
public String Type { get; set; }
public String Model { get; set; }
public Int16 Year_of_issue { get; set; }
public Int16 Seats { get; set; }
public Int16 Carrying { get; set; }
public String Maintenance { get; set; }
}
成员的嵌套列表。
class Member
{
public string Name { get; set; }
public string Position { get; set; }
public UInt16 Age { get; set; }
public UInt16 Experience { get; set; }
}
我不知道如何反序列化这个文件。 我试过这样做...
List<Airport> airport = JsonConvert.DeserializeObject<List<Airport>>(json);
但它不起作用。
Newtonsoft.Json.JsonSerializationException:无法将当前 JSON 对象(例如 {"name":"value"})反序列化为类型 'lab7.Airport',因为该类型需要 JSON 数组(例如 [1,2,3])以正确反序列化。 要修复此错误,请将 JSON 更改为 JSON 数组(例如 [1,2,3])或更改反序列化类型,使其成为普通的 .NET 类型(例如,不是原始类型像整数,而不是像数组或列表这样的集合类型),可以从 JSON 对象反序列化。 JsonObjectAttribute 也可以添加到类型以强制它从 JSON 对象反序列化。
在此先感谢您的帮助。
您可以通过此网站生成您的 类:JSON to C#
它导致此代码:
public class ListMember
{
public string Name { get; set; }
public string Position { get; set; }
public int Age { get; set; }
public int Experience { get; set; }
}
public class RootObject
{
public List<ListMember> listMember { get; set; }
public int Id { get; set; }
public string Type { get; set; }
public string Model { get; set; }
public int Year_of_issue { get; set; }
public int Seats { get; set; }
public int Carrying { get; set; }
public string Maintenance { get; set; }
}