Home > Net >  How to convert api json responce data to DateTime
How to convert api json responce data to DateTime

Time:09-29

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:

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

CodePudding user response:

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

  • Related