Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to convert api json responce data to DateTime

I recieve list of those objects frome api. I need to convert fromDate and toDate to DataTime.

  {
    "countryCode": "ago",
    "regions": [
      
    ],
    "holidayTypes": [
      "public_holiday",
      "other_day"
    ],
    "fullName": "Angola",
    "fromDate": {
      "day": 1,
      "month": 1,
      "year": 2014
    },
    "toDate": {
      "day": 31,
      "month": 12,
      "year": 32767
    }
  }

I thought, that I simply can make field with DateTime, but this didn`t work. How can I do it best way?

I serializing this way:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[HttpGet("holidays")]
public async Task<IActionResult> GetCountries(){
    var httpClient = _httpClientFactory.CreateClient("getSupportedCountries");
    var httpResponseMessage = await httpClient.GetAsync(
        "?action=getSupportedCountries");
    if (!httpResponseMessage.IsSuccessStatusCode) return BadRequest();

    using var contentStream = await httpResponseMessage.Content.ReadAsStreamAsync();
    
    var countries = await JsonSerializer.DeserializeAsync<List<Country>>(contentStream);
    
    return Ok(countries);
}

My entity class looks like this:

public class Country {
    [JsonPropertyName("countryCode")]
    public string CountryCode { get; set; }

    [JsonPropertyName("regions")]
    public List<string> Regions { get; set; }

    [JsonPropertyName("holidayTypes")]
    public List<string> HolidayTypes { get; set; }

    [JsonPropertyName("fullName")]
    public string FullName { get; set; }

    [JsonPropertyName("fromDate")]
    public DateTime FromDate { get; set; }

    [JsonPropertyName("toDate")]
    public DateTime ToDate { get; set; }
}

>Solution :

if you want to make you life much more easy , use Newtonsoft.Json

var json = await httpResponseMessage.Content.ReadAsStringAsync();

List<Country> countries = JArray.Parse(json).Select(x => x.ToObject<Country>()).ToList();

public class Country
{
    public string countryCode { get; set; }
    public List<object> regions { get; set; }
    public List<string> holidayTypes { get; set; }
    public string fullName { get; set; }

    public DateTime fromDate { get; set; }

    public DateTime toDate { get; set; }
    [JsonConstructor]
    public Country(JObject fromDate, JObject toDate)
    {
        if (fromDate != null) this.fromDate = new DateTime(Convert.ToInt32((string)fromDate["year"]),
                                                        Convert.ToInt32((string)fromDate["month"]),
                                                        Convert.ToInt32((string)fromDate["day"]));

        if (toDate != null) this.toDate = new DateTime(Convert.ToInt32((string)toDate["year"]),
                                                        Convert.ToInt32((string)toDate["month"]),
                                                        Convert.ToInt32((string)toDate["day"]));

    }
}

p.s. Max year is 9999

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading