I am trying to parse JSON data to a DataTable in C#. But while parsing I am seeing a Newtonsoft.Json.JsonSerializationException.
Error converting value "" to type 'System.DateTime'. Path '[1]['Created Date']', line 8, position 20.
Here is the code which I used to parse the data:
DataTable dtData = JArray.Parse(jsonString).ToObject<DataTable>();
Sample JSON data:
[
{
"rno": 1,
"Name": "XYZ",
"Created Date": "2014-04-30T14:39:12.2397769Z"
},
{
"rno": 2,
"Name": "ABC",
"Created Date": ""
}
]
Can someone tell me how to manage this type of scenario. I just want to prevent a data type change while adding data into the data table. Instead of DateTime, the column data type should be set as string.
created datein the second entry, do you notice anything?DataTable. That is odd. Wouldn't you rather it be converted into a list of a concrete type? Why specifically do you wantDataTable?DataTablesupportNullable<T>@Eldar?DataTablemanually and populate it manually."Created Date"is being recognized as a DateTime string -- but""is not a valid DateTime string. To disable automaticDateTimerecognition, setDateParseHandling = DateParseHandling.Noneas shown in Json.NET Disable the deserialization on DateTime. now yourDataTablewill be created with astringcolumn rather than aDateTimecolumn. You can postprocess it after deserialization to convert the column toDateTimeif necessary.