【发布时间】:2020-05-09 00:56:57
【问题描述】:
我有一个Employee表
public class Employee
{
[Key]
public long ID { get; set; }
public DateTime EmpDate { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
我创建了 Web API 来发布员工数据:
[HttpPost]
public async Task<ActionResult<Employee>> PostLead(Employee employee)
{
//Code to post
}
这是我的 JSON 正文
{
"firstname":"xyz",
"lastname":"abc",
"EmpDate":"2019-01-06 17:16:40"
}
我收到The JSON value could not be converted to System.DateTime. 的错误但是当我将 EmpDate 值作为2019-01-06 传递时,我没有收到错误。
【问题讨论】:
-
你不能把你的 EmpData 属性改成字符串吗?
-
使用 ISO 8601 格式:“2019-01-06T17:16:40.000”
-
@LinkedListT 是的,我可以。但是,还有很多其他功能依赖于 DateTime EmpDate。
-
顺便说一句,但是,假设您的 JSON 来自 JavaScript,请在 JS 端使用
Date.toISOString; ASP.NET 可以很好地处理 ISO 字符串。 -
大家好,ISO 8601 在 Postman 中为我工作。我现在将在代码中更新。谢谢大家。
标签: c# json asp.net-core-webapi json-deserialization