【发布时间】:2018-11-07 17:41:28
【问题描述】:
在 Postman 中,我通过 POST 向 API 发送以下 JSON。
{
"id": "21",
"crgName": "Walgreens - 11/07/2018 - Standard ",
"crgStarteddatetime": "2018-11-07T10:11:10",
}
...但是,我收到以下错误:FormatException: String was not Recognized as a valid DateTime.
在我的控制器中,我使用 DateTimeFormat 来格式化日期时间:
public static RemoteContextType DeserializeJsonString<RemoteContextType>(string jsonString)
{
//create an instance of generic type object
RemoteContextType obj = Activator.CreateInstance<RemoteContextType>();
MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(jsonString));
var serializer = new DataContractJsonSerializer(obj.GetType(),
new DataContractJsonSerializerSettings
{
DateTimeFormat = new
DateTimeFormat("yyyy-MM-dd'T'HH:mm:ss.fff'Z'")
});
obj = (RemoteContextType)serializer.ReadObject(ms);
ms.Close();
return obj;
}
...我的语法中是否存在关于如何格式化日期的问题?我的意图是形成日期,因为它反映在 JSON 中。关于我做错了什么,我能得到一些帮助吗?
【问题讨论】:
标签: c# json asp.net-mvc api controller