c# - 使用 JsonConvert.DeserializeObject 将 Json 反序列化为 C# POCO 类

标签 c# json serialization json.net poco

这是我的简单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;
}

这会引发异常:

Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[CoderwallDotNet.Api.Models.Account]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.

To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. Path 'accounts.github', line 1, position 129.

以前从未使用过这种 DeserializeObject 方法,我有点卡在这里。

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

我可以尝试将 JSON 反序列化到这个 POCO 类中吗?

最佳答案

这是一个工作示例。

关键点是:

  • 账户声明
  • 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;
}

关于c# - 使用 JsonConvert.DeserializeObject 将 Json 反序列化为 C# POCO 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11126242/

相关文章:

ios - 如何将 JSON 中的值附加到数组?

java - ObjectOutputStream 无法正确保存父类(super class)的字段,除非它们是可序列化的,为什么没有错误或警告?

python 3 time.sleep() "serial read"

c# - 有没有快速的方法来获得鼠标下的控件?

c# - 澄清 C# 字典上的读写

c# - 使窗口的特定部分不可见

javascript - 使用 IDE (Node) 读取本地存储的 JSON 文件

javascript - 格式错误的 JSON 响应

wcf - 是否可以在 WCF 中没有无参数构造函数的情况下序列化对象?

c# - LUIS 获取所有意图列表,其中包含所有标记的话语