[英]Cannot deserialize the current JSON object (e.g. {“name”:“value”}) into type 'System.Collections.Generic.List`1'
[英]Cannot deserialize the current JSON object (e.g. {“name”:“value”}) into type 'System.Collections.Generic.IEnumerable`1[WebApplication1.UserInfo]'
我得到这个例外。 我不知道该怎么办。 我在Google上搜索了很多,但是什么也没找到。
我正在做的是调用一些外部源API(Get调用),并希望在我的应用程序中显示它。
Json格式
{ "data": [ { "id": "bdcbec60-b8a7-4060-85d5-8a3370660d84", "first_name": "Jarin", "last_name": "Schmidt", "current_position_name": null, "current_organization_name": null, "confirmed": true, "photo_url": "https://s3.amazonaws.com/acclaim-sandbox-app/users/photos/standard/ad18cca72f6c53298dfe844a955c36e9b0bd3ace.jpg?1381944301" }, { "id": "6cfdace6-932a-4bf8-ac5c-55a1b881e8a9", "first_name": "Jonathan", "last_name": "Miranda", "current_position_name": null, "current_organization_name": null, "confirmed": true, "photo_url": null }, { "id": "9bd5a8a7-b002-404d-ae96-c8342329c9bc", "first_name": "Brent", "last_name": "Kastner", "current_position_name": null, "current_organization_name": null, "confirmed": true, "photo_url": null }, { "id": "67896042-dde3-4967-8c94-624b5b6969d3", "first_name": "Brent", "last_name": "Kastner", "current_position_name": null, "current_organization_name": null, "confirmed": true, "photo_url": "https://s3.amazonaws.com/acclaim-sandbox-app/users/photos/standard/90f1dee38456370f0fcc3a9e41aa3ade7c7d7745.jpg?1387221013" }, { "id": "98f7c4e0-6849-4048-a0c7-64fbb59a3da5", "first_name": "Christopher", "last_name": "Hjelmberg", "current_position_name": null, "current_organization_name": null, "confirmed": true, "photo_url": null }, { "id": "eec79b27-3fdc-488c-b5a3-e71ebd69ec44", "first_name": "rajesh", "last_name": "arumugam", "current_position_name": "Software Engg", "current_organization_name": "GlobalEnglish", "confirmed": true, "photo_url": "https://s3.amazonaws.com/acclaim-sandbox-app/users/photos/standard/ff5a28148397dd374626af5c9feae52e770774b2.JPG?1392875628" } ], "metadata": null }
我的代码:
HttpClient client = new HttpClient();
string baseUrl = "https://url/accessdata/userinfo.json";
client.BaseAddress = new Uri(baseUrl);
// Add an Accept header for JSON format.
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Add("Authorization", "Basic TFVBV0dkeFZlSklJZ0hjajM0M0I6");
HttpResponseMessage response = client.GetAsync(baseUrl).Result;
if (response.IsSuccessStatusCode)
{
string var = "hai";
// Parse the response body. Blocking!
var UserInfo = response.Content.ReadAsAsync<IEnumerable<UserData>>().Result;
UserData root = JsonConvert.DeserializeObject<UserData>(UserInfo);
foreach (var info in employees)
{
// logic to display in application
}
}
和Userdata类:
public class UserInfo
{
public string id { get; set; }
public string first_name { get; set; }
public string last_name { get; set; }
public string current_position_name { get; set; }
public string current_organization_name { get; set; }
public string confirmed { get; set; }
public string photo_url { get; set; }
}
public class UserData
{
public List<UserInfo> UserData { get; set; }
}
请让我知道我需要更改什么,谢谢!
问题是否可能是由于JSON中存在最终metadata
字段引起的? 请删除它,然后重试。
响应不是用户数组,而是包含data
和metadata
的对象,因此您应该更新此行:
var UserInfo = response.Content.ReadAsAsync<UserData>().Result;
和这个类:
public class UserData
{
public List<UserInfo> data { get; set; }
}
或者您可能想为正确的名称添加属性:
public class UserData
{
[JsonProperty("data")]
public List<UserInfo> UserData { get; set; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.