这是我简单的User POCO类:

/// <summary>
/// The User class represents a Coderwall User.
/// </summary>
public class User
{
    /// <summary>
    /// A User's username. eg: "sergiotapia, mrkibbles, matumbo"
    /// </summary>
    public string Username { get; set; }

    /// <summary>
    /// A User's name. eg: "Sergio Tapia, John Cosack, Lucy McMillan"
    /// </summary>
    public string Name { get; set; }

    /// <summary>
    /// A User's location. eh: "Bolivia, USA, France, Italy"
    /// </summary>
    public string Location { get; set; }

    public int Endorsements { get; set; } //Todo.
    public string Team { get; set; } //Todo.

    /// <summary>
    /// A collection of the User's linked accounts.
    /// </summary>
    public List<Account> Accounts { get; set; }

    /// <summary>
    /// A collection of the User's awarded badges.
    /// </summary>
    public List<Badge> Badges { get; set; }

}

以及用于将JSON响应反序列化为User对象(此实际JSON call is here)的方法:

private User LoadUserFromJson(string response)
{
    var outObject = JsonConvert.DeserializeObject<User>(response);
    return outObject;
}

这会引发异常:



以前从未使用过此DeserializeObject方法,我有点被困在这里。

我确保POCO类中的属性名称与JSON响应中的名称相同。

我如何尝试将JSON反序列化到此POCO类中?

最佳答案

这是一个工作示例。

关键点是:

  • Accounts的声明
  • 使用JsonProperty属性


  • using (WebClient wc = new WebClient())
    {
        var json = wc.DownloadString("http://coderwall.com/mdeiters.json");
        var user = JsonConvert.DeserializeObject<User>(json);
    }
    

    --
    public class User
    {
        /// <summary>
        /// A User's username. eg: "sergiotapia, mrkibbles, matumbo"
        /// </summary>
        [JsonProperty("username")]
        public string Username { get; set; }
    
        /// <summary>
        /// A User's name. eg: "Sergio Tapia, John Cosack, Lucy McMillan"
        /// </summary>
        [JsonProperty("name")]
        public string Name { get; set; }
    
        /// <summary>
        /// A User's location. eh: "Bolivia, USA, France, Italy"
        /// </summary>
        [JsonProperty("location")]
        public string Location { get; set; }
    
        [JsonProperty("endorsements")]
        public int Endorsements { get; set; } //Todo.
    
        [JsonProperty("team")]
        public string Team { get; set; } //Todo.
    
        /// <summary>
        /// A collection of the User's linked accounts.
        /// </summary>
        [JsonProperty("accounts")]
        public Account Accounts { get; set; }
    
        /// <summary>
        /// A collection of the User's awarded badges.
        /// </summary>
        [JsonProperty("badges")]
        public List<Badge> Badges { get; set; }
    }
    
    public class Account
    {
        public string github;
    }
    
    public class Badge
    {
        [JsonProperty("name")]
        public string Name;
        [JsonProperty("description")]
        public string Description;
        [JsonProperty("created")]
        public string Created;
        [JsonProperty("badge")]
        public string BadgeUrl;
    }
    

    10-08 11:49