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

使用JsonConvert.DeserializeObject将Json反序列化为C#POCO类,第1张

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

这是一个工作示例

关键点是:

  • 声明
    Accounts
  • JsonProperty
    属性的使用

using (WebClient wc = new WebClient()){    var json = wc.DownloadString("http://prerwall.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;}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/4894789.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-12
下一篇 2022-11-12

发表评论

登录后才能评论

评论列表(0条)

保存