如何获取JSON.NET将日期/时间序列化为ISO 8601? [英] How to get JSON.NET to serialize date/time to ISO 8601?

查看:178
本文介绍了如何获取JSON.NET将日期/时间序列化为ISO 8601?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Web API应用程序,该应用程序将JSON返回给可能没有使用Microsoft技术的使用者.当我的控制器返回具有DateTime属性作为JSON的对象时,它将以以下格式序列化日期:

I have a Web API application that returns JSON to consumers who may not be using Microsoft technologies. When my controller returns an object with DateTime properties as JSON, it serializes the date in this format:

2017-03-15T00:00:00-04:00

这让消费者有些头疼,因为他们希望它采用ISO 8601格式.一些研究告诉我,JSON.NET现在默认情况下使用ISO 8601(我使用的是9.0.1).当我运行这段代码时...

This is giving the consumer a bit of a headache as they're expect it to be in ISO 8601 format. Some research has told me that JSON.NET now uses ISO 8601 by default (I am using 9.0.1). When I run this code...

Clipboard.Copy(JsonConvert.SerializeObject(DateTime.Now));

...我明白了:

2017-03-15T09:10:13.8105498-04:00

维基百科在表示完整的日期和时间时将它们显示为有效的ISO 8601格式:

Wikipedia shows these as valid ISO 8601 formats when expressing full date and time:

2017-03-15T11:45:42+00:00
2017-03-15T11:45:42Z
20170315T114542Z

但是,我上面得到的输出与这些输出都不完全匹配.我希望格式化程序使用2017-03-15T11:45:42Z.

However, the output I got above doesn't exactly match any of those. I want the formatter to use 2017-03-15T11:45:42Z.

也许完全值得再提一个问题,在我的Web API配置中添加以下行似乎被忽略了,因为它继续在最初显示的日期中返回JSON:

And probably worthy of another question altogether, adding the below line in my Web API config seems to be ignored as it continues to return JSON in the date originally shown above:

config.Formatters.JsonFormatter.SerializerSettings.Converters.Add(new IsoDateTimeConverter());

我认为一旦确定了核心问题,Web API问题也可能得到解决.

I assume that once I figure out the core issue, the Web API issue may also be resolved.

推荐答案

获得的格式是 ISO 8601格式(请阅读Wikipedia中的时间和时区指示符"部分),您的日期显然未调整为UTC时间,因此您将在该日期之后附加一个时区偏移,而不是Z Zulu时区指示符.

The format you are getting is ISO 8601 format (read the section on Times and Time Zone Designators in Wikipedia), it's just that your dates are apparently not adjusted to UTC time, so you are getting a timezone offset appended to the date rather than the Z Zulu timezone indicator.

IsoDateTimeConverter具有可用于自定义其输出的设置.通过将DateTimeStyles设置为AdjustToUniversal,可以使其自动将日期调整为UTC.您也可以自定义输出格式,以省略小数秒.默认情况下,该转换器不适应UTC时间,并且包括秒数在内的尽可能多的精度十进制.

The IsoDateTimeConverter has settings you can use to customize its output. You can make it automatically adjust dates to UTC by setting DateTimeStyles to AdjustToUniversal. You can also customize the output format to omit the fractional seconds if you don't want them. By default, the converter does not adjust to UTC time and includes as many decimals of precision as there are available for the seconds.

尝试一下:

IsoDateTimeConverter converter = new IsoDateTimeConverter
{
    DateTimeStyles = DateTimeStyles.AdjustToUniversal,
    DateTimeFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ssK"
};

config.Formatters.JsonFormatter.SerializerSettings.Converters.Add(converter);

如果您的日期已经是UTC,但是日期上的DateTimeKind并未设置为应设置的Utc(例如,为Unspecified),那么理想情况下,您应该修复代码,以便设置此指示符正确地序列化之前.但是,如果您不能(或不想这样做),可以通过更改转换器设置以始终在日期格式中包含Z指示符(而不是使用K指示符来解决此问题)来解决该问题.查看日期上的DateTimeKind)并删除AdjustToUniversal指令.

If your dates are already UTC, but the DateTimeKind on them is not set to Utc like it should be (e.g. it is Unspecified), then ideally you should fix your code so that this indicator is set correctly prior to serializing. However, if you can't (or don't want to) do that, you can work around it by changing the converter settings to always include the Z indicator in the date format (instead of using the K specifier which looks at the DateTimeKind on the date) and removing the AdjustToUniversal directive.

IsoDateTimeConverter converter = new IsoDateTimeConverter
{
    DateTimeFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"
};

这篇关于如何获取JSON.NET将日期/时间序列化为ISO 8601?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆