如何反序列化具有基于日期的属性名称的Json?

o2rvlv0m  于 2022-12-24  发布在  其他
关注(0)|答案(2)|浏览(157)

我有下面的Json代码片段从API返回。我想反序列化到一个类型化的类,但是属性名称是日期,所以它们随着每次调用而变化。
我该怎么做呢?

"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
    }
lnvxswe2

lnvxswe21#

最简单的方法是反序列化到字典

Dictionary<DateTime, int> wattsDict = JObject.Parse(json)["watts"]
                                      .ToObject<Dictionary<DateTime, int>>();

但如果需要更多类型化数据,可以创建一个列表

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; }
}
lhcgjxsq

lhcgjxsq2#

我对newtonsoft不太熟悉,看起来好像是newtonsoft has more features than the JsonSerializer,但是我想知道如何用“内置”的System.Text.Json.Serializer来完成上面的要求,也许其他人会觉得有用
第一件事我绊倒了是

  • 日期-时间-串"2022-12-19 08:14:00"不能被开箱即用地解析,
  • 在将其改变为"2022-12-19T08:14:00"之后,其是可解析的。

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;
}

使用Dictionary<DateTime, int>是起点

Dictionary<DateTime, int> wattsDict =
              JsonSerializer.Deserialize<Dictionary<DateTime, int>>(GetWattsJson());

以便稍后将值复制到类型Watt

public class Watt
{   
    public int Counter {get; set;}
    public DateTime Date { get; set; }
    public int Quantity { get; set; }
}

复制很容易

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++;
}

linqpad中的结果

转换日期

这段代码显示了如何改变日期格式

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

相关问题