使用 JsonConvert.DeserializeObject 将 Json 反序列化为 C# POCO 类 [英] Using JsonConvert.DeserializeObject to deserialize Json to a C# POCO class

查看:38
本文介绍了使用 JsonConvert.DeserializeObject 将 Json 反序列化为 C# POCO 类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的简单 User POCO 类:

Here is my simple User POCO class:

/// <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 调用在这里):

And the method I'm using to deserialize a JSON response into a User object (this actual JSON call is here):

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

这会引发异常:

无法反序列化当前的 JSON 对象(例如 {"name":"value"})输入类型'System.Collections.Generic.List`1[CoderwallDotNet.Api.Models.Account]'因为该类型需要一个 JSON 数组(例如 [1,2,3])来反序列化正确.

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.

要修复此错误,请将 JSON 更改为 JSON 数组(例如[1,2,3])或更改反序列化类型,使其成为正常.NET 类型(例如,不是像整数这样的原始类型,不是集合类型(如数组或列表),可以从 JSON 反序列化目的.也可以将 JsonObjectAttribute 添加到类型中以强制它从 JSON 对象反序列化.路径accounts.github",第 1 行,位置 129.

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 方法,我有点卡在这里了.

Having never worked with this DeserializeObject method before, I'm kind of stuck here.

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

I've made sure that the property names in the POCO class are the same as the names in the JSON response.

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

What can I try to deserialize JSON into this POCO class?

推荐答案

这是一个工作示例.

关键点是:

  • 账户的声明
  • JsonProperty 属性的使用
  • Declaration of Accounts
  • Use of JsonProperty attribute

.

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

这篇关于使用 JsonConvert.DeserializeObject 将 Json 反序列化为 C# POCO 类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆