簡體   English   中英

如何反序列化具有基於日期的屬性名稱的 Json?

[英]How do I deserialize Json that has date based property names?

我有以下從 API 返回的 Json 片段。 我想反序列化為 Typed 類,但是屬性名稱是日期,因此它們會隨着每次調用而改變。

我該怎么做呢?

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

最簡單的方法是反序列化為字典

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

我不熟悉並且 newtonsoft 似乎比 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM