使用JsonConvert.DeserializeObject反序列化JSON来一个C#类POCO [英] Using JsonConvert.DeserializeObject to deserialize Json to a C# POCO class

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

问题描述

下面是我简单的用户 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响应到用户对象(这实际的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对象(例如{名:值})
  入式
  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?

推荐答案

下面是一个工作的例子。

Here is a working example.

关键点是:


  • 声明帐户

  • 使用 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天全站免登陆