1

I got some strange behavior when reading a json-file with C#.

My json-file contains some dates:

 "status": "Open",
 "priority": "Medium",
 "created": "2013-11-13T18:40:36.000+0000",
 "updated": "2014-1-2T10:45:17.000+0000",
 "duedate": "2013-12-31T0:0:0.000+0000",

I read the file via

string json = File.ReadAllText(fileName)

// the following line does not matter (just for reference)
// issue already exists when viewing the json string variable above
JObject mappedObj = JObject.Parse(File.ReadAllText(fileName));

My json string now contains

 "status": "Open",
 "priority": "Medium",
 "created": "11/13/2013 19:40:36",
 "updated": "2014-1-2T10:45:17.000+0000",
 "duedate": "2013-12-31T0:0:0.000+0000",

It's just the "created" field that does not behave like it should.

Interesting part: This does not only happen after deserialization, the wrong date shows up, when i view the "json" string with the .net debugging json-viewer (not with the text-viewer!). As you can see, i just display the content read from the file.

Any ideas? Interestingly the code worked before reinstalling Windows. Maybe it's some localization issue. I want my code to be localization indepentent anyway. Any suggestions would be highly appreciated :)

Follow-Up-Question

Ok, it's now obvoius that the dates, that are not converted, do not accord to ISO-standard.

Do you know if there is a way to avoid the parser recognizing the string as date? If i want to serialize again, it keeps outputting the "13.11.2013" string instead of the ISO-Notation i need.

I use

JsonConvert.SerializeObject(inforData, new IsoDateTimeConverter())
Christian Huber
  • 828
  • 1
  • 5
  • 20
  • 1
    Change the datetime strings as follows `"updated": "2014-01-02T10:45:17.000+0000", "duedate": "2013-12-31T00:00:00.000+0000"` , you'll see all of them behave like *created* – EZI Aug 03 '15 at 11:59
  • 1
    To add onto that: The second and third dates are malformed ISO8601 notation. Month and day should be 0-padded, as well as hour, minute and second. – Mark Jansen Aug 03 '15 at 12:09
  • 1
    "created" is [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) datetime format. "updated" and "duedate" are not. Therefore "created" recognizes as a date and time, "updated" and "duedate" as a simple string. – Alexander Petrov Aug 03 '15 at 12:11
  • Ok, i see the now obvious, thanks :) I added a follow-up-question, do you have an idea? – Christian Huber Aug 03 '15 at 12:16
  • 1
    This should help: [On the nightmare that is JSON Dates](http://www.hanselman.com/blog/OnTheNightmareThatIsJSONDatesPlusJSONNETAndASPNETWebAPI.aspx) – Liam Aug 03 '15 at 12:23
  • don't use `IsoDateTimeConverter`. try `var json=mappedObj.ToString()` – EZI Aug 03 '15 at 12:24

0 Answers0