Search code examples
c#jsonjson-deserializationsystem.text.json

Custom deserialize from JSON navigating the response


I have a API that returns a JSON like the one below and I have to deserialize it in a C# object:

    {
      "found": true,
      "resultsData": [
        {
          "2023-05-13": [
            {
              "info1": "Test",
              "infoCode": 1,
              "infos": [
                {
                  "code": 2,
                  "extraInfo1": "test1",
                  "extraInfo2": "test2",
                  "extraInfo3": "test3",
                  "order": 1,
                  "risk": "",
                  "alarm": "alarm text"
                },
                {
                  "code": 3,
                  "extraInfo1": "test1",
                  "extraInfo2": "test2",
                  "extraInfo3": "test3",
                  "order": 1,
                  "risk": "",
                  "alarm": "alarm text"
                },
                {
                  "code": 4,
                  "extraInfo1": "test1",
                  "extraInfo2": "test2",
                  "extraInfo3": "test3",
                  "order": 1,
                  "risk": "",
                  "alarm": "alarm text"
                },
                {
                  "code": 5,
                  "extraInfo1": "test1",
                  "extraInfo2": "test2",
                  "extraInfo3": "test3",
                  "order": 1,
                  "risk": "",
                  "alarm": "alarm text"
                }
              ]
            }
          ]
        },
        {
          "2023-05-14":  [
            {
              "info1": "Test",
              "infoCode": 2,
              "infos": [
                {
                  "code": 6,
                  "extraInfo1": "test1",
                  "extraInfo2": "test2",
                  "extraInfo3": "test3",
                  "order": 1,
                  "risk": "",
                  "alarm": "alarm text"
                },
                {
                  "code": 7,
                  "extraInfo1": "test1",
                  "extraInfo2": "test2",
                  "extraInfo3": "test3",
                  "order": 1,
                  "risk": "",
                  "alarm": "alarm text"
                },
                {
                  "code": 8,
                  "extraInfo1": "test1",
                  "extraInfo2": "test2",
                  "extraInfo3": "test3",
                  "order": 1,
                  "risk": "",
                  "alarm": "alarm text"
                },
                {
                  "code": 9,
                  "extraInfo1": "test1",
                  "extraInfo2": "test2",
                  "extraInfo3": "test3",
                  "order": 1,
                  "risk": "",
                  "alarm": "alarm text"
                }
              ]
            }
          ]
        }
      ],
      "message": ""
    }

If the "2023-05-13" is inserted inside the object like a property it could easy to deserialize that into a object but because this isn't deserializable into POCO object and it's not possible to change the API response I try to find a solution. Following this article from Microsoft I understand that I can deserialize manually using System.Text.Json and the JsonNode object and read property and value in a cicle loop. I figure out how to obtain a Json node and object but I'm stucked for retrieve the "2023-05-13" value and the "2023-05-14" because they haven't a "name" like the others properties. Any help with some code to figure out will be much more than appreciated


Solution

  • you can use a Dictionary to get DateTime data

         Data data = System.Text.Json.JsonSerializer.Deserialize<Data>(json);
    
    public partial class Data
    {
        [JsonPropertyName("found")]
        public bool Found { get; set; }
    
        [JsonPropertyName("resultsData")]
        public List<Dictionary<string, List<ResultsData>>> ResultsData { get; set; }
    
        [JsonPropertyName("message")]
        public string Message { get; set; }
    }
    
    public partial class ResultsData
    {
        [JsonPropertyName("info1")]
        public string Info1 { get; set; }
    
        [JsonPropertyName("infoCode")]
        public long InfoCode { get; set; }
    
        [JsonPropertyName("infos")]
        public List<Info> Infos { get; set; }
    }
    
    public partial class Info
    {
        [JsonPropertyName("code")]
        public long Code { get; set; }
    
        [JsonPropertyName("extraInfo1")]
        public string ExtraInfo1 { get; set; }
    
        [JsonPropertyName("extraInfo2")]
        public string ExtraInfo2 { get; set; }
    
        [JsonPropertyName("extraInfo3")]
        public string ExtraInfo3 { get; set; }
    
        [JsonPropertyName("order")]
        public long Order { get; set; }
    
        [JsonPropertyName("risk")]
        public string Risk { get; set; }
    
        [JsonPropertyName("alarm")]
        public string Alarm { get; set; }
    }