Supporting multiple custom DateTime formats when deserializing with Json.Net(使用 Json.Net 反序列化时支持多种自定义 DateTime 格式)
问题描述
我想支持使用 Newtonsoft Json 反序列化器反序列化多个自定义 DateTime 格式,所以我使用 IsoDateTimeConverter:
I want to support deserializing more than one custom DateTime format with the Newtonsoft Json deserializer, so I am using IsoDateTimeConverter:
var serializeSettings = new JsonSerializerSettings();
serializeSettings.Converters.Add(new IsoDateTimeConverter() { DateTimeFormat = "yyyyMMddTHHmmssZ" });
由于 DateTimeFormat 属性不接受格式数组,我尝试了以下方法来支持多种自定义日期格式:
As the DateTimeFormat property does not accept an array of formats, I tried the following to support multiple custom date formats:
var serializeSettings = new JsonSerializerSettings();
serializeSettings.Converters.Add(new IsoDateTimeConverter() { DateTimeFormat = "yyyyMMddTHHmmssZ" });
serializeSettings.Converters.Add(new IsoDateTimeConverter() { DateTimeFormat = "yyyy-MM-ddTHH:mm" });
但是上面的代码结果只支持反序列化第一种格式.
However the above code's result is supporting deserializing only the first format.
如何实现对多种自定义 DateTime 格式的支持?
How can I achieve supporting multiple custom DateTime formats?
推荐答案
如果您想处理多种可能的日期格式,您需要制作一个可以接受多种格式字符串的自定义 JsonConverter 并尝试直到成功为止.这是一个简单的例子:
If you want to handle multiple possible date formats, you will need to make a custom JsonConverter which can accept multiple format strings and try them all until one succeeds. Here is a simple example:
class MultiFormatDateConverter : JsonConverter
{
public List<string> DateTimeFormats { get; set; }
public override bool CanConvert(Type objectType)
{
return objectType == typeof(DateTime) || objectType == typeof(DateTime?);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
string dateString = (string)reader.Value;
if (dateString == null)
{
if (objectType == typeof(DateTime?))
return null;
throw new JsonException("Unable to parse null as a date.");
}
DateTime date;
foreach (string format in DateTimeFormats)
{
// adjust this as necessary to fit your needs
if (DateTime.TryParseExact(dateString, format, CultureInfo.InvariantCulture, DateTimeStyles.None, out date))
return date;
}
throw new JsonException("Unable to parse "" + dateString + "" as a date.");
}
public override bool CanWrite
{
get { return false; }
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
然后你可以像这样将它添加到你的设置中:
Then you can add it to your settings like this:
var settings = new JsonSerializerSettings();
settings.DateParseHandling = DateParseHandling.None;
settings.Converters.Add(new MultiFormatDateConverter
{
DateTimeFormats = new List<string> { "yyyyMMddTHHmmssZ", "yyyy-MM-ddTHH:mm" }
});
小提琴:https://dotnetfiddle.net/vOpMEY
这篇关于使用 Json.Net 反序列化时支持多种自定义 DateTime 格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 Json.Net 反序列化时支持多种自定义 DateTime 格式
基础教程推荐
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
