简体   繁体   English

无法将当前JSON对象(例如{“ name”:“ value”})反序列化为类型“ System.Collections.Generic.IEnumerable`1 [WebApplication1.UserInfo]”

[英]Cannot deserialize the current JSON object (e.g. {“name”:“value”}) into type 'System.Collections.Generic.IEnumerable`1[WebApplication1.UserInfo]'

Im getting this exception. 我得到这个例外。 I don't understand what should i do. 我不知道该怎么办。 I have googled a lot but found nothing. 我在Google上搜索了很多,但是什么也没找到。

What I am doing is calling some external source API (Get call) and like to display it in my application. 我正在做的是调用一些外部源API(Get调用),并希望在我的应用程序中显示它。

Json format 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 }

My code: 我的代码:

 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
                 }
        }

and the Userdata class: 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; }
}

please let me know what do i need to change Thanks in advance! 请让我知道我需要更改什么,谢谢!

Is is possible that the problem is due to the presence of final metadata field in your JSON? 问题是否可能是由于JSON中存在最终metadata字段引起的? Please remove it and try again. 请删除它,然后重试。

Response is not array of users, it's object containing data and metadata , so you should update this row: 响应不是用户数组,而是包含datametadata的对象,因此您应该更新此行:

   var UserInfo = response.Content.ReadAsAsync<UserData>().Result;

And this class: 和这个类:

   public class UserData
   {
       public List<UserInfo> data { get; set; }
   }

Or you might wanna add attribute for correct name: 或者您可能想为正确的名称添加属性:

   public class UserData
   {
       [JsonProperty("data")]
       public List<UserInfo> UserData { get; set; }
   }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法将当前JSON对象(例如{“ name”:“ value”})反序列化为类型&#39;System.Collections.Generic.List`1&#39; - Cannot deserialize the current JSON object (e.g. {“name”:“value”}) into type 'System.Collections.Generic.List`1' 无法将当前 JSON object(例如 {"name":"value"})反序列化为类型 'System.Collections.Generic.List`1 - Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1 无法反序列化当前JSON对象(例如{“ name”:“ value”})为类型&#39;System.Collections.Generic.List - Cannot deserialize the current JSON object (e.g. {“name”:“value”}) into type 'System.Collections.Generic.List 无法将当前JSON对象(例如{“ name”:“ value”})反序列化为类型“ System.Collections.Generic.ICollection`1 [System.Double]” - Cannot deserialize the current JSON object (e.g. {“name”:“value”}) into type 'System.Collections.Generic.ICollection`1[System.Double]' JSON.Net - 无法将当前json对象(例如{“name”:“value”})反序列化为类型&#39;system.collections.generic.list`1 - JSON.Net - cannot deserialize the current json object (e.g. {“name”:“value”}) into type 'system.collections.generic.list`1 JsonSerializationException:无法将当前 JSON object(例如 {"name":"value"})反序列化为类型 'System.Collections。 - JsonSerializationException: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List 无法将当前 JSON 对象(例如 {&quot;name&quot;:&quot;value&quot;})反序列化为类型 &#39;System.Collections.Generic.IList 问题 - Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.IList problem 错误:无法将当前 JSON object(例如 {“name”:“value”})反序列化为类型 'System.Collections.Generic.List`1 - Error: Cannot deserialize the current JSON object (e.g. {“name”:“value”}) into type 'System.Collections.Generic.List`1 JSON .NET-将当前JSON对象(例如{“ name”:“ value”})反序列化为类型&#39;System.Collections.Generic.List`1 - JSON .NET - annot deserialize the current JSON object (e.g. {“name”:“value”}) into type 'System.Collections.Generic.List`1 无法将当前JSON数组(例如[1,2,3])反序列化为类型&#39;System.Collections.Generic.Dictionary - Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'System.Collections.Generic.Dictionary
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM