我已经试着让它工作了4天了,但我似乎就是想不出来。我正在通过api接收json字符串,并希望将数据解析为不同的字符串和into:
Json字符串示例:
"[{\"Entry_number\":\"4\",\"Entry_date\":\"2019-01-10 18:22:55\",\"Customer_number\":\"16\",\"Entry_value\":\"13\",\"Comment\":\"Nu gaat ie t GVD doen\"},
{\"Entry_number\":\"5\",\"Entry_date\":\"2019-01-12 14:34:23\",\"Customer_number\":\"16\",\"Entry_value\":\"10\",\"Comment\":\"TextBox\"},
{\"Entry_number\":\"6\",\"Entry_date\":\"2019-01-12 14:34:31\",\"Customer_number\":\"16\",\"Entry_value\":\"10\",\"Comment\":\"Onrustig\"},
{\"Entry_number\":\"7\",\"Entry_date\":\"2019-01-12 14:34:37\",\"Customer_number\":\"16\",\"Entry_value\":\"10\",\"Comment\":\"Ziek\"}]"
我想要转换成:
public class SleepEntry
{
string Entry_number;
string Entry_date;
string Customer_number;
string Entry_value;
string Comment;
public string Entry_number1 { get => Entry_number; set => Entry_number = value; }
public string Entry_date1 { get => Entry_date; set => Entry_date = value; }
public string Customer_number1 { get => Customer_number; set => Customer_number = value; }
public string Entry_value1 { get => Entry_value; set => Entry_value = value; }
public string Comment1 { get => Comment; set => Comment = value; }
}
public class SleepData
{
private List<SleepEntry> plants;
public List<SleepEntry> Plants { get => plants; set => plants = value; }
}
读取json的代码:
public void Get_Monthly_Data()
{
StreamReader reader = new StreamReader(@"C:\Users\Ruben\source\repos\Health App Goede\Health App Goede\Resources\Cutstomer_ID.txt");
Customernumber = reader.ReadLine();
reader.Close();
//string json = entrys();
//convert json to series objects
SleepData sleepdata = JsonConvert.DeserializeObject<SleepData>(entrys());
}
public string entrys()
{
string json = get.get_customer_monitoring_entry("sleep", Customernumber);
return json;
}
现在,我想尝试检查每个条目是否为本周的日期。然后,从本周开始获取每个条目的Entry_value,现在将其显示在文本框中。任何帮助或提示都将不胜感激。
发布于 2019-01-13 16:12:46
您的Json字符串是一个数组,因此需要使用
var result = JsonConvert.DeserializeObject<IEnumerable<SleepEntry>>(str);
其中SleepEntry被修改为
public class SleepEntry
{
string Entry_number;
string Entry_date;
string Customer_number;
string Entry_value;
string Comment;
[JsonProperty("Entry_number")]
public string Entry_number1 { get => Entry_number; set => Entry_number = value; }
[JsonProperty("Entry_date")]
public string Entry_date1 { get => Entry_date; set => Entry_date = value; }
[JsonProperty("Custom_number1")]
public string Customer_number1 { get => Customer_number; set => Customer_number = value; }
[JsonProperty("Entry_value")]
public string Entry_value1 { get => Entry_value; set => Entry_value = value; }
[JsonProperty("Comment")]
public string Comment1 { get => Comment; set => Comment = value; }
}
PS:您可以随时删除支持字段并使用自动属性,除非您已将其用于特定目的
用于获取当前周的第一个条目的值。
var today = DateTime.Now;
var startOfWeek = today.AddDays((int)DateTime.Now.DayOfWeek * -1);
var value = result.Where(x=>DateTime.Parse(x.Entry_date1) > startOfWeek
&& DateTime.Parse(x.Entry_date1) < today.AddDays(7))
.Select(x=>x.Entry_value1).FirstOrDefault();
https://stackoverflow.com/questions/54167097
复制相似问题