Lucian Bumb Lucian Bumb - 5 months ago 32
JSON Question

How to deserialize object with date in C#

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.

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

Fiddle