c# - 用于一系列日期的日期字符串
问题描述
我在使用此功能时遇到问题,基本上我从两个不同的日历中选择两个日期,在文本框中打印这些日期,然后我想获得对存储在 JSON 中的一些信息的访问权限(就像在注释行中所做的那样)分别地。我试图从 selectedDate_start 和 selectedDate_end 之间的每一天获取每个 CountyInfectionNumber 和每个发病率。
如您所见,格式有点不稳定,但这就是它在我的 JSON 文件中的编写方式。
我的问题在于这两行:
DateTime loadedDate_start = DateTime.ParseExact(chosenDate_start, "d", null);
DateTime loadedDate_end = DateTime.ParseExact(chosenDate_end, "d", null);
我究竟做错了什么?它说 selectedDate_ 不能是字符串,但这不是 ParseExact 应该做的吗?
@insane_developer 在评论中解决了这个问题,支持他
{
using (var webClient = new System.Net.WebClient())
{
json = webClient.DownloadString("https://datelazi.ro/latestData.json");
}
var semiParsedData = JObject.Parse(json);
string chosenDate_start;
string chosenDate_end;
string chosenCounty;
ZiAleasaatunci.Text = Calendar_de_Ales_Inc.SelectionRange.Start.ToString("yyyy-MM-dd");
ZiSfarsitAleasaAtunci.Text = Calendar_Ales_Sf.SelectionRange.End.ToString("yyyy-MM-dd");
chosenDate_start = ZiAleasaatunci.Text;
chosenDate_end = ZiSfarsitAleasaAtunci.Text;
chosenCounty = JudeteAtunci.Text;
//var countySpecificInfections = semiParsedData.SelectToken($"historicalData.{chosenDate_start}.countyInfectionsNumbers.{chosenCounty}");
//var countySpecificIncidence = semiParsedData.SelectToken($"historicalData.{chosenDate_start}.incidence.{chosenCounty}");
//NumarCazuriAtunci.Text = countySpecificInfections.Value<int>().ToString();
//IncidentaAtunci.Text = countySpecificIncidence.Value<double>().ToString();
DateTime loadedDate_start = DateTime.ParseExact(chosenDate_start, "d", null);
DateTime loadedDate_end = DateTime.ParseExact(chosenDate_end, "d", null);
List<DateTime> allDates = new List<DateTime>();
DateTime Zi_Aleasa = loadedDate_start ;
DateTime Zi_Precedenta = Zi_Aleasa.AddDays(-1);
Console.WriteLine(Zi_Precedenta);
for (DateTime date = loadedDate_start; date <= loadedDate_end; date = date.AddDays(1))
allDates.Add(date);
}
解决方案
推荐阅读
- python - Python:提取文本文件中两个字符串之间的值
- javascript - 将徽标放在导航栏上会影响导航栏上的整个文本
- ios - iPhone 11 上前置位置的默认 AVCaptureDevice 太“宽”
- android - Audio filnger 中的混音器线程是如何创建的,以及 app 和 audio flinger 混音器之间的映射是如何完成的?
- arrays - 线性代数可变自由度力向量的最佳和最有效格式?
- python - 如何将实例属性初始化为 numpy 数组?
- file - 使用 SharePlum for Sharepoint 下载文件
- linux - 如果匹配显示成功或失败,如何匹配文件中的语法
- javascript - 使用自动换行在 React 中渲染逗号分隔的链接
- c++ - 为什么我得到 nan 作为下面代码的输出?