C# - 无法将 JSON 反序列化为列表 <T>
C# - Cannot deserialize JSON into List<T>
我正在使用 GET 请求获取 BD 中的条目列表(我正在使用 RestSharp 发出请求)并且我想将响应的内容反序列化为一个列表。
列表元素的类型是"Mileage":
public class Mileage
{
public int BranchId { get; set; }
public int RouteId { get; set; }
public int Travels { get; set; }
public int Monday { get; set; }
public int Tuesday { get; set; }
public int Wednesday { get; set; }
public int Thursday { get; set; }
public int Friday { get; set; }
public int Saturday { get; set; }
public int Sunday { get; set; }
}
而 response.Content 是这样的:
{
"TheoreticalMileages": [
{
"BranchId": 36,
"RouteId": 1860,
"Travels": 10,
"Monday": 132,
"Tuesday": 89,
"Wednesday": 92,
"Thursday": 104,
"Friday": 112,
"Saturday": 0,
"Sunday": 79
},
{
"BranchId": 7,
"RouteId": 2600,
"Travels": 12,
"Monday": 40,
"Tuesday": 30,
"Wednesday": 40,
"Thursday": 100,
"Friday": 121,
"Saturday": 130,
"Sunday": 0
},
{
"BranchId": 23,
"RouteId": 33,
"Travels": 8,
"Monday": 54,
"Tuesday": 50,
"Wednesday": 35,
"Thursday": 50,
"Friday": 67,
"Saturday": 32,
"Sunday": 30
}
],
"TotalRecords": 3
}
我正在使用 Newtonsoft JSON.NET 将对象反序列化为里程列表。我已经尝试将其转换为 List 和 IEnumerable:
var mileages = JsonConvert.DeserializeObject<IEnumerable<Mileage>>(getAllResponse.Content);
但是我在两种类型的集合中都遇到了同样的错误:"Cannot deserialize the current json object because the type requires a json array[...]"
有没有办法将对象直接转换成列表?提前致谢。
您使用的 JSON 不是列表。它是一个对象。它的 属性 之一恰好是一个列表。像这样:
public class MileageContainer
{
public IEnumerable<Mileage> TheoreticalMileages { get; set; }
public int TotalRecords { get; set; }
}
然后你应该能够将 JSON 反序列化为该对象:
JsonConvert.DeserializeObject<MileageContainer>(getAllResponse.Content);
我正在使用 GET 请求获取 BD 中的条目列表(我正在使用 RestSharp 发出请求)并且我想将响应的内容反序列化为一个列表。
列表元素的类型是"Mileage":
public class Mileage
{
public int BranchId { get; set; }
public int RouteId { get; set; }
public int Travels { get; set; }
public int Monday { get; set; }
public int Tuesday { get; set; }
public int Wednesday { get; set; }
public int Thursday { get; set; }
public int Friday { get; set; }
public int Saturday { get; set; }
public int Sunday { get; set; }
}
而 response.Content 是这样的:
{
"TheoreticalMileages": [
{
"BranchId": 36,
"RouteId": 1860,
"Travels": 10,
"Monday": 132,
"Tuesday": 89,
"Wednesday": 92,
"Thursday": 104,
"Friday": 112,
"Saturday": 0,
"Sunday": 79
},
{
"BranchId": 7,
"RouteId": 2600,
"Travels": 12,
"Monday": 40,
"Tuesday": 30,
"Wednesday": 40,
"Thursday": 100,
"Friday": 121,
"Saturday": 130,
"Sunday": 0
},
{
"BranchId": 23,
"RouteId": 33,
"Travels": 8,
"Monday": 54,
"Tuesday": 50,
"Wednesday": 35,
"Thursday": 50,
"Friday": 67,
"Saturday": 32,
"Sunday": 30
}
],
"TotalRecords": 3
}
我正在使用 Newtonsoft JSON.NET 将对象反序列化为里程列表。我已经尝试将其转换为 List 和 IEnumerable:
var mileages = JsonConvert.DeserializeObject<IEnumerable<Mileage>>(getAllResponse.Content);
但是我在两种类型的集合中都遇到了同样的错误:"Cannot deserialize the current json object because the type requires a json array[...]"
有没有办法将对象直接转换成列表?提前致谢。
您使用的 JSON 不是列表。它是一个对象。它的 属性 之一恰好是一个列表。像这样:
public class MileageContainer
{
public IEnumerable<Mileage> TheoreticalMileages { get; set; }
public int TotalRecords { get; set; }
}
然后你应该能够将 JSON 反序列化为该对象:
JsonConvert.DeserializeObject<MileageContainer>(getAllResponse.Content);