C# JSON 中的 ASP.NET Web API 日期格式未成功序列化

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12936614/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 01:00:29  来源:igfitidea点击:

ASP.NET Web API Date format in JSON does not serialise successfully

c#serializationasp.net-web-apijson.net

提问by bstack

All,

全部,

We are using ASP.NET Web API where we have a REST based service with JSON for the payload. If I pass the following Date as a string e.g

我们正在使用 ASP.NET Web API,其中我们有一个基于 REST 的服务,有效负载为 JSON。如果我将以下日期作为字符串传递,例如

sampleObj: {
...
myDate: "31/12/2011 00:00:00",
...
}

as an attribute value in the JSON payload, the date attribute gets deserialised into a DateTime.MinValue. Is the string format valid?

作为 JSON 负载中的属性值,日期属性被反序列化为 DateTime.MinValue。字符串格式是否有效?

We know the format "2012-10-17 07:45:00" serialises successfully but we cannot guarantee that all dates received will be in this format. What are the valid options?

我们知道格式“2012-10-17 07:45:00”可以成功序列化,但我们不能保证收到的所有日期都是这种格式。什么是有效的选项?

采纳答案by Maggie Ying

In ASP.NET Web API, you can add different Json.NET DateTimeConverters through the JsonFormatter's SerializerSettings to make your service understand different DateTime format.

在 ASP.NET Web API 中,您可以通过 JsonFormatter 的 SerializerSettings 添加不同的 Json.NET DateTimeConverters,使您的服务理解不同的 DateTime 格式。

However, I do not think there is a default DateTimeConverter from Json.NET that takes in this format "31/12/2011 00:00:00". In this case you implement your custom DateTimeConverter.

但是,我认为 Json.NET 中没有采用“31/12/2011 00:00:00”格式的默认 DateTimeConverter。在这种情况下,您实现自定义 DateTimeConverter。

WebApiConfig.cs:

WebApiConfig.cs:

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

Custom DateTimeConverter:

自定义日期时间转换器:

public class MyDateTimeConverter : DateTimeConverterBase
{
    //...
}  

For more information about how to write a custom DateTimeConverter, I found something on stackoverflow that you might find useful: How to create a json.net Date to String custom Converter.

有关如何编写自定义 DateTimeConverter 的更多信息,我在 stackoverflow 上找到了一些您可能会觉得有用的内容: How to create a json.net Date to String custom Converter。

回答by claudiu.nicola

Just set globalization in web.config:

只需在 web.config 中设置全球化:

<globalization enableClientBasedCulture="false" requestEncoding="utf-8" responseEncoding="utf-8" culture="en-GB" uiCulture="en-GB"/>

and then, in Global.asax.cs > Application_Start, set JsonFormatter to use the current culture:

然后,在 Global.asax.cs > Application_Start 中,将 JsonFormatter 设置为使用当前区域性:

var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
json.SerializerSettings.Culture = System.Globalization.CultureInfo.CurrentCulture;