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

时间:2014-04-08 05:39:11

标签: c# json wcf serialization asp.net-web-api

我得到了这个例外。我不明白我该怎么办。我搜索了很多但没有找到任何东西。

我正在做的是调用一些外部源API(Get call)并喜欢在我的应用程序中显示它。

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

请让我知道我需要改变什么 提前谢谢!

2 个答案:

答案 0 :(得分:0)

问题是否可能是由于JSON中存在最终metadata字段?请将其删除,然后重试。

答案 1 :(得分:0)

响应不是用户数组,它包含datametadata的对象,因此您应该更新此行:

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