6

I try to parse simple JSON using Json.net

 string inputJson = @"
                {
                 ""modificationTime"" : ""\/Date(1224043200000)\/""
                 }";

And property is defined

[JsonProperty("modificationTime")]
[JsonConverter(typeof(JavaScriptDateTimeConverter))]
public DateTime ModificationTime { get; set; }

But DeserializeObject throw an exception with the following Message: "Unexpected token or value when parsing date. Token: Date, Value: 10/15/2008 04:00:00"

Well, as far as I see it actually has parsed the date, hasn't it? This exception is thrown from the line 68 in the JavaScriptDateTimeConverter.cs:

68 if (reader.TokenType != JsonToken.StartConstructor ||  string.Compare(reader.Value.ToString(), "Date", StringComparison.Ordinal) != 0)
69            throw new Exception("Unexpected token or value when parsing date. Token: {0}, Value: {1}".FormatWith(CultureInfo.InvariantCulture, reader.TokenType, reader.Value));
70    
71          reader.Read();

In this place reader.TokenType is Date and reader.Value.ToString() is 10/15/2008 04:00:00. Any ideas?

2 Answers 2

8

Json.NET deserializes dates with the format:

"\/Date(1224043200000)\/"

by default. JavaScriptDateTimeConverter is for dates with the format:

new Date(1234567890)
Sign up to request clarification or add additional context in comments.

Comments

0

I had similar problem some time ago. I found a workaround and blogged about it:

http://shico.blogspot.com/2010/07/ajaxpro-deserialize-json-datetime.html

Hope it helps.

1 Comment

No, it doesn't. He wrote that if it is "/Date(1224043200000)/" than it will not work. While I use ""\/Date(1224043200000)\/"". I suppose the problem is reader.TokenType should be JsonToken.StartConstructor. And it probably means that "\/ is parsed like this token.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.