0

I'm trying to Deserialize a date correctly from json.

The date format is like this:

"created_at": "2015-11-29T13:15:27+00:00",

I have tried something like:

var result = Newtonsoft.Json.JsonConvert.DeserializeObject<Root>(json, new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" });

(as well as a few other formats) But not got it to work

dotnetfiddle:

https://dotnetfiddle.net/iB0H8I

EDIT

looks like the issue lies with underscores in the field names - how best to handle this given that I don't have control of the json?

raklos
  • 28,027
  • 60
  • 183
  • 301
  • 2
    I think this is an issue with System.Text.Json not liking underscores. If you rename the field in the JSON, removing the underscore and then do the same for for the JsonPropertyName attribute e.g. createdat then it works. – Kev Ritchie Jun 11 '21 at 23:55
  • @KevRitchie I just noticed that too, would have thought this is a common one to handle. I am not responsible for the json so not sure if there is a proper way to handle this – raklos Jun 11 '21 at 23:56
  • This may help - https://stackoverflow.com/questions/59914611/attribute-jsonproperty-works-incorrect-with-net-core-3-1-when-i-use-underscore – Kev Ritchie Jun 12 '21 at 00:03
  • Does this answer your question? [C# JSON.NET convention that follows Ruby property naming conventions?](https://stackoverflow.com/questions/3922874/c-sharp-json-net-convention-that-follows-ruby-property-naming-conventions) – Brett Caswell Jun 12 '21 at 04:13
  • 1
    fyi, for your code sample (which I removed all the unnecessary json and classes to minimal reproduce this) would like this: https://dotnetfiddle.net/rkHhAA with the accepted answer in that linked question – Brett Caswell Jun 12 '21 at 04:27
  • actually, upon reviewing this.. you're just using the wrong attribute to set the property name here. you should update this question to include the class definition, since that is the issue here. – Brett Caswell Jun 12 '21 at 04:42
  • removing my previous duplicate comment: as the current answer can be a valid approach for your scenario.. as such, consider https://stackoverflow.com/questions/15915503/net-newtonsoft-json-deserialize-map-to-a-different-property-name as a possible solution here as well. – Brett Caswell Jun 12 '21 at 05:01

2 Answers2

2

Turns out System.Text.Json isn’t the issue here, but more the solution.

I’ve removed the using for Newtonsoft.Json.Converters and replaced it with System.Text.Json. Then used the JsonSerializer to Deserialize.

Also removed the IsoDateTimeConverter code.

This seems to work - https://dotnetfiddle.net/UEE3QD

Kev Ritchie
  • 1,599
  • 10
  • 16
  • so, upon review - this seems to be an issue with determining on `JsonPropertyAttribute` vs `JsonPropertyNameAttribute` as it relates to these different methods on these different types. As such, I think this approach is a good solution if OP doesn't intend to modify attributes on their type. Otherwise, they could just add `JsonProperty("created_at")` to the property to resolve. – Brett Caswell Jun 12 '21 at 04:57
1

In case that you want to keep the date in the universal world format (yyyy-mm-dd hh:mm:ss am/pm), you can use ToString() function.

Here is the example:

https://dotnetfiddle.net/R1jFbe

Thomson Mixab
  • 657
  • 4
  • 8