Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Exception occurs while deserialize json containing date with different format C#

Having json string containing date field

{
  "totalSize": 2,
  "records": [
    {
      "Id": "5006C000008ZhEDQA0",
      "CreatedDate": "2021-12-01T15:14:20.000+0000",
      "CaseNumber": "01378682",
      "Status": "Open"
    },
    {
      "Id": "5006C000008ZhE00A0",
      "CreatedDate": "2021-12-05T08:00:00.000+0000",
      "CaseNumber": "01378692",
      "Status": "Open"
    }
  ]
}

I’m trying to do normal Deserialization where CreatedDate datatype is DateTime.

JsonSerializer.Deserialize<SFHistoryResponse>(stringResponse);

I’m getting

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The JSON value could not be converted to System.DateTime. Path:
$.records[0].CreatedDate

is there any way to format JSON’s date part before Deserialization

>Solution :

The Newtonsoft JSON library can deserialize the string properly without throwing an exception:

using Newtonsoft.Json;

var response =  JsonConvert.DeserializeObject<SFHistoryResponse>(stringResponse);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading