C# 反序列化客户端 AJAX JSON 日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/82058/
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
Deserializing Client-Side AJAX JSON Dates
提问by Brian Chavez
Given the following JSON Date representation:
给定以下 JSON 日期表示:
"\/Date(1221644506800-0700)\/"
How do you deserialize this into it's JavaScript Date-type form?
你如何将它反序列化为它的 JavaScript 日期类型形式?
I've tried using MS AJAX JavaScrioptSerializer as shown below:
我试过使用 MS AJAX JavaScrioptSerializer,如下所示:
Sys.Serialization.JavaScriptSerializer.deserialize("\/Date(1221644506800-0700)\/")
However, all I get back is the literal string date.
但是,我得到的只是文字字符串日期。
采纳答案by Simon_Weaver
Provided you know the string is definitely a date I prefer to do this :
如果您知道该字符串绝对是我更喜欢这样做的日期:
new Date(parseInt(value.replace("/Date(", "").replace(")/",""), 10))
回答by Daniel
The big number is the standard JS time
大数是标准的JS时间
new Date(1221644506800)
Wed Sep 17 2008 19:41:46 GMT+1000 (EST)
2008 年 9 月 17 日星期三 19:41:46 GMT+1000 (EST)
回答by Sjoerd Visscher
A JSON value is a string, number, object, array, true, false or null. So this is just a string. There is no official way to represent dates in JSON. This syntax is from the asp.net ajax implementation. Others use the ISO 8601 format.
JSON 值是字符串、数字、对象、数组、true、false 或 null。所以这只是一个字符串。没有官方的方法来表示 JSON 中的日期。此语法来自 asp.net ajax 实现。其他人使用 ISO 8601 格式。
You can parse it like this:
你可以这样解析:
var s = "\/Date(1221644506800-0700)\/";
var m = s.match(/^\/Date\((\d+)([-+]\d\d)(\d\d)\)\/$/);
var date = null;
if (m)
date = new Date(1*m[1] + 3600000*m[2] + 60000*m[3]);
回答by Kyle Jones
The regular expression used in the ASP.net AJAX deserialize method looks for a string that looks like "/Date(1234)/" (The string itself actually needs to contain the quotes and slashes). To get such a string, you will need to escape the quote and back slash characters, so the javascript code to create the string looks like "\"\/Date(1234)\/\"".
ASP.net AJAX 反序列化方法中使用的正则表达式查找类似于“/Date(1234)/”的字符串(字符串本身实际上需要包含引号和斜杠)。要获得这样的字符串,您需要对引号和反斜杠字符进行转义,因此创建字符串的 javascript 代码看起来像 "\"\/Date(1234)\/\""。
This will work.
这将起作用。
Sys.Serialization.JavaScriptSerializer.deserialize("\"\/Date(1221644506800)\/\"")
It's kind of weird, but I found I had to serialize a date, then serialize the string returned from that, then deserialize on the client side once.
这有点奇怪,但我发现我必须序列化一个日期,然后序列化从中返回的字符串,然后在客户端反序列化一次。
Something like this.
像这样的东西。
Script.Serialization.JavaScriptSerializer jss = new Script.Serialization.JavaScriptSerializer();
string script = string.Format("alert(Sys.Serialization.JavaScriptSerializer.deserialize({0}));", jss.Serialize(jss.Serialize(DateTime.Now)));
Page.ClientScript.RegisterStartupScript(this.GetType(), "ClientScript", script, true);
回答by Alex Nolasco
For those who don't want to use Microsoft Ajax, simply add a prototype function to the string class.
对于那些不想使用 Microsoft Ajax 的人,只需在 string 类中添加一个原型函数即可。
E.g.
例如
String.prototype.dateFromJSON = function () {
return eval(this.replace(/\/Date\((\d+)\)\//gi, "new Date()"));
};
Don't want to use eval? Try something simple like
不想使用 eval?尝试一些简单的东西
var date = new Date(parseInt(jsonDate.substr(6)));
As a side note, I used to think Microsoft was misleading by using this format. However, the JSON specification is not very clear when it comes to defining a way to describe dates in JSON.
作为旁注,我曾经认为微软使用这种格式会产生误导。但是,JSON 规范在定义一种用 JSON 描述日期的方法时并不是很清楚。
回答by ESV
Bertrand LeRoy, who worked on ASP.NET Atlas/AJAX, described the design of the JavaScriptSerializer DateTime outputand revealed the origin of the mysterious leading and trailing forward slashes. He made this recommendation:
从事 ASP.NET Atlas/AJAX 工作的 Bertrand LeRoy描述了 JavaScriptSerializer DateTime 输出的设计,并揭示了神秘的前导斜杠和尾随正斜杠的起源。他提出了这样的建议:
run a simple search for "\/Date((\d+))\/" and replace with "new Date($1)" before the eval (but after validation)
运行一个简单的搜索 "\/Date((\d+))\/" 并在 eval 之前替换为 "new Date($1)"(但在验证之后)
I implemented that as:
我将其实现为:
var serializedDateTime = "\/Date(1271389496563)\/";
document.writeln("Serialized: " + serializedDateTime + "<br />");
var toDateRe = new RegExp("^/Date\((\d+)\)/$");
function toDate(s) {
if (!s) {
return null;
}
var constructor = s.replace(toDateRe, "new Date()");
if (constructor == s) {
throw 'Invalid serialized DateTime value: "' + s + '"';
}
return eval(constructor);
}
document.writeln("Deserialized: " + toDate(serializedDateTime) + "<br />");
This is very close to the many of the other answers:
这与许多其他答案非常接近:
- Use an anchored RegEx as Sjoerd Visscher did -- don't forget the ^ and $.
- Avoid string.replace, and the 'g' or 'i' options on your RegEx. "/Date(1271389496563)//Date(1271389496563)/" shouldn't work at all.
- 像 Sjoerd Visscher 那样使用锚定的 RegEx——不要忘记 ^ 和 $。
- 避免使用 string.replace 以及 RegEx 上的“g”或“i”选项。"/Date(1271389496563)//Date(1271389496563)/" 根本不应该工作。
回答by tavo
Actually, momentjs supports this kind of format, you might do something like:
实际上,momentjs 支持这种格式,你可以这样做:
var momentValue = moment(value);
momentValue.toDate();
This returns the value in a javascript date format
这以 javascript 日期格式返回值