C# Json过滤输出
如果在过去24小时内(C# Json过滤输出,c#,.net,json.net,C#,.net,Json.net,如果在过去24小时内(s.Begin日期的格式如下2018-05-19T04:39:59Z),如果s.Service\u name等于“cloud\u networking”,我将如何过滤输出(Console.WriteLine(statusCollection…) class Program { private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.Method
s.Begin
日期的格式如下2018-05-19T04:39:59Z),如果s.Service\u name
等于“cloud\u networking”,我将如何过滤输出(Console.WriteLine(statusCollection…
)
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; }
}
类程序
{
私有静态只读log4net.ILog log=log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
//公共静态void Main()
公共静态void Main(字符串[]args)
{
使用(var webClient=new webClient())
{
String rawJSON=webClient.DownloadString(“https://status.cloud.google.com/incidents.json");
List statusCollection=JsonConvert.DeserializeObject(rawJSON);
Console.WriteLine(statusCollection.Count+“上次运行:”+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}更新”,
s、 开始,s.编号,s.严重性,s.服务(名称,s.外部(描述,s.更新.计数));
log.Info(DateTime.Now.ToString(“\n MM/dd/yyyy h:MM tt”);
}
}
}
公共阶级地位
{
公共字符串Begin{get;set;}
已创建公共字符串{get;set;}
公共字符串结束{get;set;}
公共字符串外部描述{get;set;}
公共字符串已修改{get;set;}
[JsonProperty(“最新更新”)]
公共MRUpdateContainer最近更新{get;set;}
公共整数{get;set;}
公共bool public{get;set;}
公共字符串服务_键{get;set;}
公共字符串服务_name{get;set;}
公共字符串严重性{get;set;}
公共列表更新{get;set;}
公共字符串Uri{get;set;}
}
公共类MRUpdateContainer
{
已创建公共字符串{get;set;}
公共字符串已修改{get;set;}
公共字符串文本{get;set;}
当{get;set;}时的公共字符串
}
公共类更新
{
已创建公共字符串{get;set;}
公共字符串已修改{get;set;}
公共字符串文本{get;set;}
当{get;set;}时的公共字符串
}
(1)您可以将Begin
属性类型更改为DateTime
,以便更容易根据它进行筛选。Json.Net ISO 8601 dates to DateTime:
public DateTime Begin { get; set; }
(2) 反序列化收集后,可以使用Linq-操作筛选数据,如下所示:
DateTime start = DateTime.Now.AddHours(-24);
statusCollection = statusCollection.Where(r => r.Service_name == "cloud_networking" && r.Begin > start).ToList();
最常见的是,LINQ用于过滤集合。在尝试使用LINQ的Where时遇到了什么问题?我看到您使用的是Select,所以您都知道并使用它LINQ@CamiloTerevinto你好,卡米洛,我尝试了Console.WriteLine(statusCollection.Select)(s.Service\u name=“cloud\u networking”…但这一点运气都不好。我对.net相当陌生,所以我一直在学习,感谢您抽出时间来帮助我。一切都很顺利,在此过程中,我从您那里学到了一些新技术。