[英]How do I deserialize Json that has date based property names?
I have the following Json snippet being returned from an API.我有以下从 API 返回的 Json 片段。 I want to deserialize into a Typed class however the property names are dates so they change with each call.
我想反序列化为 Typed 类,但是属性名称是日期,因此它们会随着每次调用而改变。
How do I do this?我该怎么做呢?
"watts": {
"2022-12-19 08:14:00": 0,
"2022-12-19 09:00:00": 48,
"2022-12-19 10:00:00": 114,
"2022-12-19 11:00:00": 140,
"2022-12-19 12:00:00": 140,
"2022-12-19 13:00:00": 132,
"2022-12-19 14:00:00": 105,
"2022-12-19 15:00:00": 53,
"2022-12-19 15:44:00": 0,
"2022-12-20 08:14:00": 0,
"2022-12-20 09:00:00": 230,
"2022-12-20 10:00:00": 383,
"2022-12-20 11:00:00": 453,
"2022-12-20 12:00:00": 453,
"2022-12-20 13:00:00": 384,
"2022-12-20 14:00:00": 238,
"2022-12-20 15:00:00": 81,
"2022-12-20 15:44:00": 0
}
the easiest way is to deserialize to a dictionary最简单的方法是反序列化为字典
Dictionary<DateTime, int> wattsDict = JObject.Parse(json)["watts"]
.ToObject<Dictionary<DateTime, int>>();
but if you need more typed data, you can create a list但如果您需要更多类型的数据,您可以创建一个列表
List<Watt> watts = ((JObject)JObject.Parse(json)["watts"]).Properties()
.Select(i => new Watt { Date = DateTime.Parse(i.Name),
Quantity = (int)i.Value }
).ToList();
public class Watt
{
public DateTime Date { get; set; }
public int Quantity { get; set; }
}
I am not familiar with newtonsoft and it seem that newtonsoft has more features than the JsonSerializer .我不熟悉newtonsoft并且 newtonsoft 似乎比 JsonSerializer 具有更多功能。 Nonetheless i wondered how the requirement above could be done with the "built-in" System.Text.Json.Serializer .
尽管如此,我想知道如何使用“内置” System.Text.Json.Serializer来完成上述要求。 And maybe others might find it usefull
也许其他人会觉得它有用
The first thing i stumbled over was that我偶然发现的第一件事是
"2022-12-19 08:14:00"
could not be parsed out of the box,"2022-12-19 08:14:00"
,"2022-12-19T08:14:00"
it is parseable."2022-12-19T08:14:00"
后,它是可解析的。 GetWattsJson()
returns the json object with the changed date format. GetWattsJson()
返回具有更改日期格式的 json 对象。
public static string GetWattsJson()
{
// This format "2022-12-19 08:14:00" resulted in "JSON value not supported format."
// This format "2008-03-12T11:07:31" works out of the box
string json ="""
{
"2022-12-19T08:14:00": 0,
"2022-12-19T09:00:00": 48,
"2022-12-19T10:00:00": 114,
"2022-12-19T13:00:00": 132,
"2022-12-20T08:14:00": 0,
"2022-12-20T11:00:00": 453,
"2022-12-20T15:44:00": 0
}
""";
return json;
}
Using a Dictionary<DateTime, int>
is the starting point使用
Dictionary<DateTime, int>
是起点
Dictionary<DateTime, int> wattsDict =
JsonSerializer.Deserialize<Dictionary<DateTime, int>>(GetWattsJson());
to later copy the values into the type Watt
稍后将值复制到
Watt
类型中
public class Watt
{
public int Counter {get; set;}
public DateTime Date { get; set; }
public int Quantity { get; set; }
}
The copying is quite easy复制非常容易
List<Watt> wattList = new List<Watt>();
int i = 0;
foreach (var (key, value) in wattsDict)
{
wattList.Add(new Watt{Counter = i, Date = key, Quantity = value});
i++;
}
This code show how to change the date format此代码显示如何更改日期格式
string timeString = "2022-12-19 08:14:00";
var dateVal = DateTime.ParseExact(timeString
, "yyyy-MM-dd HH:mm:ss"
, CultureInfo.InvariantCulture);
string output1 = dateVal.ToString("s"); // 2022-12-19T08:14:00
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.