【问题标题】:Cannot parse Reddit Json [duplicate]无法解析 Reddit Json [重复]
【发布时间】:2017-02-15 17:37:33
【问题描述】:

我正在尝试使用 c# 和 json.net 解析来自 [https://reddit.com/new/.json] 的 json。问题是每个帖子的 json 都不同,我需要知道是否有办法对 json 进行动态反序列化。有人吗?

【问题讨论】:

    标签: c# json.net json-deserialization


    【解决方案1】:

    返回的 JSON 是否保持相同的对象名称?只是有时它可能是空白的或可能有值?

    如果相同的对象总是在那里,你可以做这样的事情,它应该适合你。

    public class Account
    {
        public string Email { get; set; }
        public bool Active { get; set; }
        public DateTime CreatedDate { get; set; }
        public IList<string> Roles { get; set; }
    }
    
    string json = @"{
      'Email': 'james@example.com',
      'Active': true,
      'CreatedDate': '2013-01-20T00:00:00Z',
      'Roles': [
        'User',
        'Admin'
      ]
    }";
    
    Account account = JsonConvert.DeserializeObject<Account>(json);
    
    Console.WriteLine(account.Email);
    // james@example.com
    

    http://www.newtonsoft.com/json/help/html/DeserializeObject.htm

    【讨论】:

    • Rfjt 感谢您的回答,问题是json响应发生了变化。模式不清楚。那是我的难处。但是我尝试了一种新方法,而不是解析接收到的 json,我选择了一些基本属性并解析响应。工作。感谢您的回归。
    猜你喜欢
    • 1970-01-01
    • 2015-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-31
    • 2020-06-17
    • 1970-01-01
    • 2011-01-29
    相关资源
    最近更新 更多