How to parse ASP.NET JSON Date format with GWT

醉酒当歌 提交于 2020-01-11 09:14:05

问题


ASP.NET JSON serialize DateTime to the following format "/Date(1251877601000)/". Pls, help parse this string into the java(GWT) Date object.

At this time the solution I came with is parsing with regex, extract long.. but then I cannot push long through JSNI.


回答1:


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);
   }



回答2:


The answer to this question is, use nuget to obtain JSON.NET then use this inside your JsonResult method:

return Json(JsonConvert.SerializeObject(/* JSON OBJECT TO SEND TO VIEW */));

inside your view simple do this in javascript:

JSON.parse(@Html.Raw(Model.data))

If it comes via a view model that is or if it is an ajax call:

var request = $.ajax({ url: "@Url.Action("SomeAjaxAction", "SomeController")", dataType: "json"});
request.done(function (data, result) { JSON.parse(data); });


来源:https://stackoverflow.com/questions/1367899/how-to-parse-asp-net-json-date-format-with-gwt

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!