Convert to DateTime in json.net

I am trying to deserialize the following:

{"ts":"2012-04-22 04:14:50,669", "msg":"Hello"}

at

public class LogEntry
{
    public DateTime Ts { get; set; }
    public string Msg { get; set; }
}

using

var logEntry = JsonConvert.DeserializeObject<LogEntry>(line);

But get a JsonSerializationException that says: "{" Error converting value "2012-04-22 04:14:28,478 \" to enter "System.DateTime". Line 1, position 31. "}. I cannot change the format of the log.

I think that I may need to parse the date string myself using a converter. However, I cannot find examples JsonConverterthat seem relevant. In particular, how to read the value from readerin the method ReadJson.

Are there any simple examples I should look at? Or am I mistaken about this?

+3
source share
2 answers

DateTime (,478). JsonSerializerSettings ( ) Culture, , DeserializeObject<T>(value, settings) ( ). , InvariantCulture.

+5

, , , , , , , (, , ). , , ?

-1

All Articles