C# 从 .Net Web 方法修复 JSON 日期序列化
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18022258/
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
Fixing JSON Date serialization from .Net web method
提问by Justin
I am currently working on a project where I am sending a .Net type via ajax to a client application via ajax. I have no issues with the object being serialized and set to the client.
我目前正在开发一个项目,我通过 ajax 将 .Net 类型通过 ajax 发送到客户端应用程序。我对被序列化并设置为客户端的对象没有任何问题。
I run into issues when I take the exactly same object and post it back to the server via a web method with the following error: /Date(1373950800000)/ is not a valid value for DateTime.
Which is pretty annoying as that is how Microsoft gave it to me, but that's besides the point.
当我获取完全相同的对象并通过 Web 方法将其发回服务器时遇到了以下错误:/Date(1373950800000)/ is not a valid value for DateTime.
这很烦人,因为 Microsoft 是这样给我的,但这不是重点。
Does anyone have a quick fix for this? I want a seamless way this can be accomplished without having to change the object right before returning it from the ajax call.
有没有人可以快速解决这个问题?我想要一种无缝的方式来实现这一点,而不必在从 ajax 调用返回它之前更改对象。
采纳答案by Karl Anderson
Your issue comes down to the server-side JavaScript serializer you are using; either JsonDataContractSerializer
(default serializer for ASP.NET MVC) or NewtonSoft Json Serializer
(default serializer for ASP.NET Web API).
您的问题归结为您使用的服务器端 JavaScript 序列化程序;无论是JsonDataContractSerializer
(ASP.NET MVC的默认的序列)或NewtonSoft Json Serializer
(对于的ASP.NET Web API默认的序列)。
For a visual example of this date mangling issue as well as possible solutions, check out JSON Dates are Different in ASP.NET MVC and Web API.
有关此日期修改问题的直观示例以及可能的解决方案,请查看ASP.NET MVC 和 Web API 中的 JSON 日期不同。
回答by kul_mi
回答by CurlyPaul
This is the method that I use for these things:
这是我用于这些事情的方法:
First you need to clean the junk out of the date parameter
首先你需要清除日期参数中的垃圾
String unixDate = "/Date(1373950800000)/";
unixDate = unixDate.Replace("/Date(","").Replace(")/", "");
Now, as .NET and unix measure time in a different way, you have to compensate for that by creating a date set to the 1st of Jan 1970 and then adding the numeric part of the date that you were passed
现在,由于 .NET 和 unix 以不同的方式测量时间,您必须通过创建一个设置为 1970 年 1 月 1 日的日期,然后添加您通过的日期的数字部分来弥补这一点
DateTime dotNetDate = new DateTime(1970, 1, 1);
dotNetDate = dotNetDate.AddMilliseconds(Convert.ToInt64(unixDate)
You should also note that there will a loss of precision here - .NET dates are to the nearest nanosecond where unix dates are to the nearest millisecond
您还应该注意,这里会丢失精度 - .NET 日期精确到纳秒,而 unix 日期精确到毫秒
回答by Jeff Ogata
Try sending the dates with the forward slashes escaped. I have an iPad client that posts JSON to an ASP.NET WebAPI service method and we have to send dates this way:
尝试发送带有转义斜杠的日期。我有一个 iPad 客户端,它将 JSON 发布到 ASP.NET WebAPI 服务方法,我们必须以这种方式发送日期:
"due": "\/Date(1335830400000)\/"
"due": "\/Date(1335830400000)\/"
回答by Rao khurram adeel
Handle the DateFormat while serialization following code will resolve your problem
处理 DateFormat 而序列化以下代码将解决您的问题
JsonConvert.SerializeObject(yourobject, Formatting.Indented,
new JsonSerializerSettings
{
DateFormatHandling = DateFormatHandling.IsoDateFormat
});
It will result the dates in 2009-02-15T00:00:00Z format
它将导致 2009-02-15T00:00:00Z 格式的日期