I am having a JSON with a date time in MM/dd/yyyy HH:mm:ss format, for example, 12/20/2000 10:30:00.
The complete JSON will be something similar to this.
[
{
"id": 10001,
"name": "Name 1",
"date": "10/01/2022 00:00:00"
},
{
"id": 10002,
"name": "Name 2",
"date": "10/01/2022 00:00:00"
},
{
"id": 10003,
"name": "Name 3",
"date": "10/01/2022 00:00:00"
}
]
I have a c# class with id, name and date where the date is a DateTime type.
I am trying to convert the JSON object to a list of Objects, but I am getting a conversion error as shown below.
System.AggregateException: One or more errors occurred. (Unable to deserialize response content to the type of List`1)rn ---> System.Exception: Unable to deserialize response content to the type of List`1rn
If I remove the date from the class, everything works fine.
If I convert the date to a string type, it works. But I need to keep it as DateTime.
2
Answers
you can do this, with DateTime properties you need to provide the JsonConverter.
DeserializeObject Declaration:
Reference: https://www.newtonsoft.com/json/help/html/P_Newtonsoft_Json_Converters_IsoDateTimeConverter_DateTimeFormat.htm
this works for me