【发布时间】:2010-11-24 23:18:13
【问题描述】:
ASP.NET JSON 将 DateTime 序列化为以下格式“/Date(1251877601000)/”。请帮助将此字符串解析为 java(GWT) Date 对象。
此时我提出的解决方案是用正则表达式解析,提取长..但是我无法通过 JSNI 长推。
【问题讨论】:
ASP.NET JSON 将 DateTime 序列化为以下格式“/Date(1251877601000)/”。请帮助将此字符串解析为 java(GWT) Date 对象。
此时我提出的解决方案是用正则表达式解析,提取长..但是我无法通过 JSNI 长推。
【问题讨论】:
这个问题的答案是,使用 nuget 获取 JSON.NET,然后在你的 JsonResult 方法中使用它:
return Json(JsonConvert.SerializeObject(/* JSON OBJECT TO SEND TO VIEW */));
在您的视图中简单地在javascript 中执行此操作:
JSON.parse(@Html.Raw(Model.data))
如果它来自一个视图模型,或者它是一个 ajax 调用:
var request = $.ajax({ url: "@Url.Action("SomeAjaxAction", "SomeController")", dataType: "json"});
request.done(function (data, result) { JSON.parse(data); });
【讨论】:
function FixJsonDates(data) {
//microsoft script service perform the following to fix the dates.
//json date:\/Date(1317307437667-0400)\/"
//javasccript format required: new Date(1317307437667-0400)
//copied from micrsoft generated fiel.
var _dateRegEx = new RegExp('(^|[^\\\\])\\"\\\\/Date\\((-?[0-9]+)(?:[a-zA-Z]|(?:\\+|-)[0-9]{4})?\\)\\\\/\\"', 'g');
var exp = data.replace(_dateRegEx, "$1new Date($2)");
return eval(exp);
}
【讨论】: