【发布时间】:2018-11-02 09:27:41
【问题描述】:
无法将 webapi 值读取到列表对象。请查看链接http://prntscr.com/lcmw7q处的截图值。
错误是 Newtonsoft.Json.JsonSerializationException: '无法将当前 JSON 对象(例如 {"name":"value"})反序列化为类型 'System.Collections.Generic.List`1[ StudentProfileClient.StudentInfo]',因为该类型需要 JSON 数组(例如 [1,2,3])才能正确反序列化。 要修复此错误,请将 JSON 更改为 JSON 数组(例如 [1,2,3])或更改反序列化类型,使其成为普通的 .NET 类型(例如,不是像整数这样的原始类型,而不是像可以从 JSON 对象反序列化的数组或列表。 JsonObjectAttribute 也可以添加到类型中以强制它从 JSON 对象反序列化。 路径“数据”,第 2 行,位置 10。'
class StudentInfo
{
public string cellPhone { get; set; }
public string gender { get; set; }
public string dateOfBirth { get; set; }
}
class RootClass
{
public int TotalCount { get; set; }
public List<StudentInfo> stdinfo { get; set; }
}
static async Task GetData()
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://203.81.70.102:8092/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response;
response = await client.GetAsync("control/qZkNkcGgWq6PiVbJzQ2=/student-profiles");
if (response.IsSuccessStatusCode)
{
List<StudentInfo> RootClass = new List<StudentInfo>();
RootClass = await response.Content.ReadAsAsync<List<StudentInfo>>();
}
}
}
【问题讨论】:
-
您不能使用 JsonConvert.DeserializeObject 从您的 API 响应中直接反序列化。
-
你能告诉我我应该在哪里更正我的代码..我会非常有帮助
-
这些都是假的。感谢您的提醒...
-
@Md.RayhanKhan,你能显示你的 json 吗?
标签: c# asp.net-web-api