How to deserialize object with date in C#

10,714
var obj = JsonConvert.DeserializeObject<Rootobject>(jsonString, 
            new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" });

Fiddle

Share:
10,714
Lucian Bumb
Author by

Lucian Bumb

I like C#, Asp.Net MVC and angular and blazor. Programming is a hobby for me

Updated on June 24, 2022

Comments

  • Lucian Bumb
    Lucian Bumb almost 2 years

    I have this Json from a web api:

    jsonstring ={"users":[{"id":1123,"last_update":"2016-02-28 14:53:04"}],"page":1,"pages":1}
    

    which I want to deserialize in an object like:

    public class Rootobject
    {
        public User[] users { get; set; }
        public int page { get; set; }
        public int pages { get; set; }
    }
    
    public class User
    {
        public int id { get; set; }
        public DateTime last_update { get; set; }
    }
    

    for this I use:

     var obj= JsonConvert.DeserializeObject<Rootobject>(jsonString);
    

    the result has null for last_update.

    jsonstring is a string result from WebClient.DownloadString(url); which look like above example.

    How can I get the date on deserialization?

    Edit:

    None of the solutions from this post Deserializing dates with dd/mm/yyyy format using Json.Net help me fix my issue.