我尝试了两种不同的配置,以便解析DataTime 2017-10-17T17:22:50.658+02:00
。其他解决方案与不使用DataContractJsonSerializer
有关,但它不是我的解决方案。
我已经尝试过的配置:
DateTime example = DateTime.ParseExact(
"2017-10-17T17:22:50.658+02:00",
"yyyy-MM-ddTHH:mm:ss.fffzzz",
System.Globalization.CultureInfo.InvariantCulture);
未在设置中指定任何日期时间格式
private static DataContractJsonSerializerSettings GetSerializerSettings()
{
var settings = new DataContractJsonSerializerSettings();
settings.UseSimpleDictionaryFormat = true;
settings.EmitTypeInformation = System.Runtime.Serialization.EmitTypeInformation.Never;
return settings;
}
我收到DateTime '2017-10-18T13:32:42.753+02:00' are not startingwith '\/Date(' and ending with ')\/' as JSON requires
错误消息。
在设置中指定日期时间格式
private static DataContractJsonSerializerSettings GetSerializerSettings()
{
var settings = new DataContractJsonSerializerSettings();
settings.DateTimeFormat = System.Runtime.Serialization.DateTimeFormat("yyyy-MM-ddTHH:mm:ss.fffzzz");
settings.UseSimpleDictionaryFormat = true;
settings.EmitTypeInformation = System.Runtime.Serialization.EmitTypeInformation.Never;
return settings;
}
我收到Can't recognize as valid DateTime
错误消息。
任何提示?
答案 0 :(得分:0)
尝试:
DataContractJsonSerializerSettings settings = new DataContractJsonSerializerSettings
{
DateTimeFormat = new DateTimeFormat("yyyy-MM-ddTHH:mm:ss.fffK")
};