【问题标题】:Newtonsoft.Json filtering output [duplicate]Newtonsoft.Json过滤输出[重复]
【发布时间】:2019-09-13 00:51:27
【问题描述】:

我将如何过滤输出 (Console.WriteLine(statusCollection...) 以仅在过去 24 小时内 s.Service_name 等于 "cloud_networking" 时显示结果(s.Begin 日期格式如下 2018-05- 19T04:39:59Z

class Program
{
    private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

    //public static void Main()
    public static void Main(string[] args)
    {
        using (var webClient = new WebClient())
        {

            String rawJSON = webClient.DownloadString("https://status.cloud.google.com/incidents.json");
            List<Status> statusCollection = JsonConvert.DeserializeObject<List<Status>>(rawJSON);
            Console.WriteLine(statusCollection.Count + "Last Run:" + DateTime.Now.ToString("MM/dd/yyyy h:mm tt"));
            //Console.WriteLine(statusCollection.Count + " items");

            Console.WriteLine(string.Join("\n", statusCollection.Select(s => string.Format("{0} {1} ({2}) {3} - {4} - {5} updates",
                                                   s.Begin, s.Number, s.Severity, s.Service_name, s.External_desc, s.Updates.Count))));
            log.Info(DateTime.Now.ToString("\n MM/dd/yyyy h:mm tt"));
        }
    }
}

public class Status
{
    public string Begin { get; set; }
    public string Created { get; set; }
    public string End { get; set; }
    public string External_desc { get; set; }
    public string Modified { get; set; }
    [JsonProperty("most-recent-update")]
    public MRUpdateContainer Most_recent_update { get; set; }
    public int Number { get; set; }
    public bool Public { get; set; }
    public string Service_key { get; set; }
    public string Service_name { get; set; }
    public string Severity { get; set; }
    public List<Update> Updates { get; set; }
    public string Uri { get; set; }
}

public class MRUpdateContainer
{
    public string Created { get; set; }
    public string Modified { get; set; }
    public string Text { get; set; }
    public string When { get; set; }
}
public class Update
{
    public string Created { get; set; }
    public string Modified { get; set; }
    public string Text { get; set; }
    public string When { get; set; }
}

【问题讨论】:

  • 最常见的是,LINQ 用于过滤集合。您在尝试使用 LINQ 的 Where 时遇到了什么问题?我看到你正在使用 Select 所以你都知道并使用 LINQ
  • @CamiloTerevinto 嗨 camilo,我尝试了 Console.WriteLine(statusCollection.Select(s.Service_name = "cloud_networking"... 但没有运气。我对 .net 还很陌生,所以我我在学习,因为我得到了很长的时间

标签: c# .net json.net


【解决方案1】:

(1) 您可以将Begin 属性类型更改为DateTime,以便根据它进行过滤。 Json.Net can deserialize ISO 8601 日期为 DateTime:

public DateTime Begin { get; set; }

(2) 反序列化集合后,可以使用Linq .Where 过滤数据,操作如下:

DateTime start = DateTime.Now.AddHours(-24);
statusCollection = statusCollection.Where(r => r.Service_name == "cloud_networking" && r.Begin > start).ToList();

【讨论】:

  • 感谢您抽出宝贵时间帮助我。一切都很完美,在这个过程中我从你那里学到了一些新技术。
猜你喜欢
  • 2020-04-23
  • 1970-01-01
  • 2018-02-02
  • 1970-01-01
  • 1970-01-01
  • 2020-02-13
  • 1970-01-01
  • 2014-06-15
  • 2021-07-02
相关资源
最近更新 更多