简体   繁体   中英

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.

What I am doing is calling some external source API (Get call) and like to display it in my application.

Json format

{ "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:

 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? 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM