skip to Main Content

I have the following Json snippet being returned from an API. I want to deserialize into a Typed class however the property names are dates so they change with each call.

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
    }

2

Answers


  1. 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; }
    }
    
    Login or Signup to reply.
  2. I am not familiar with and it seem that newtonsoft has more features than the JsonSerializer. Nonetheless i wondered how the requirement above could be done with the "built-in" System.Text.Json.Serializer. And maybe others might find it usefull

    The first thing i stumbled over was that

    • the date-time-string "2022-12-19 08:14:00" could not be parsed out of the box,
    • after changing it to "2022-12-19T08:14:00" it is parseable.

    GetWattsJson() returns the json object with the changed date format.

    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> wattsDict =
                  JsonSerializer.Deserialize<Dictionary<DateTime, int>>(GetWattsJson());
    

    to later copy the values into the type 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++;
    }
    

    Result in

    linqpad demo

    Convert Date

    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
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search