反序列化 Json 时日期格式切换

问题描述

我有一个填充数据表的后台进程。它通过从 Json 格式的 API 检索少量行并将它们反序列化为数据表(循环)来工作。然后将这些行添加到另一个数据表中,该数据表将包含 API 不再有结果后的所有行。

现在发生的情况是,在对 API 进行大约 3-4 次调用后,客户端中的 JsonConvert 将行反序列化为与之前的行不同的日期格式。然后在尝试将它们添加到主数据表时出错。

错误: 字符串未被识别为有效的 DateTime。无法将 存储在 交货日期列。预期类型为 DateTime。"

我检查了 api 中的数据库格式和 Json 格式,这些都在 yyyy-MM-dd 中。但是在这 3-4 次调用 JsonConvert 之后突然使用 MM/dd/yyyy。

我尝试将 JsonSerializerSettings 添加到转换器、更改 Windows 区域设置并强制使用 CultureInfo,但是这些都不起作用。

allData = JsonConvert.DeserializeObject<DataTable>(jsonString);                        

foreach (DataRow dr in allData.Rows)
{
    tempTable.Rows.Add(dr.ItemArray);
}

'jsonString' 变量包含来自 api 的 Json。 tempTable 是包含所有行的数据表。

编辑:jsonString 示例:

 {
    "id": 2453,"shoppingcartid": 2453,"creationdate": "2019-08-06T00:00:00","ordernumber": 2738,"ordernumberStr": "CS358405","invoicenumberStr": "10724761","customername": "ITT","customerid": 1182,"managerid": 5186,"enduser": null,"enduserid": null,"location": null,"locationid": null,"state": "Open","stateid": 1,"type1": "T4 21.9 LZ","type1specification": 2,"vehicle": "NVT","vehiclespec": 12,"deliverydate": "2019-12-20T00:00:00","start": "2019-12-09T00:00:00","end": "2019-12-20T00:00:00","isplanned": false,"desireddeliverydate": "2019-12-20T00:00:00"
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...