How to parse JSON to receive a Date object in Java

2019-01-01 08:49发布

I have a following piece of JSON:

\/Date(1293034567877)\/

which is a result of this .NET code:

var obj = DateTime.Now;
var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
serializer.Serialize(obj).Dump();

Now the problem I am facing is how to create a Date object from this in JavaScript. All I could find was incredible regex solution (many containing bugs).

It is hard to believe there is no elegant solution as this is all in JavaScrip, I mean JavaScript code trying to read JSON (JavaScript Object Notation) which is supposed to be a JavaScript code and at this moment it turns out it's not cause JavaScript cannot do a good job here.

I've also seen some eval solutions which I could not make to work (besides being pointed out as security threat).

Is there really no way to do it in an elegant way?

Similar question with no real answer:
How to parse ASP.NET JSON Date format with GWT

17条回答
只靠听说
2楼-- · 2019-01-01 09:30

You can convert JSON Date to normal date format in JavaScript.

var date = new Date(parseInt(jsonDate.substr(6)));
查看更多
梦寄多情
3楼-- · 2019-01-01 09:31

As Callum mentioned, for me, the best way is to change the Controller method to string instead of JsonResult".

public string GetValues()
{
  MyObject.DateFrom = DateTime.Now;
  return JsonConvert.SerializeObject(MyObject);
}

From the ajax method you can do something like this

 $.ajax({
 url: "/MyController/GetValues",
 type: "post",
 success: function (data) {
 var validData = JSON.parse(data);
//if you are using datepicker and you want set a format
$("#DateFrom").val($.datepicker.formatDate("dd/mm/yy", new Date(validData.DateFrom)));                                      
// if you want the date as returned
$("#DateFrom").val(new Date(validData.DateFrom))
}
});
查看更多
人间绝色
4楼-- · 2019-01-01 09:32

AngularJS couldn't parse .NET JSON date /Date(xxxxxxxxxxxxx)/ string either..

I side stepped this issue by formatting the date to its ISO 8601 string representation instead of dumping the Date object directly...

Here is a sample of ASP.NET MVC code..

return Json(new { 
  date : DateTime.Now.ToString("O") //ISO 8601 Angular understands this format
});

I tried RFC 1123 but it doesn't work.. Angular treats this as string instead of Date.

return Json(new { 
  date : DateTime.Now.ToString("R") //RFC 1123 Angular won't parse this
});
查看更多
刘海飞了
5楼-- · 2019-01-01 09:35

I've not used .Net for things like this. If you were able to get it to print something like the following out it should work.

Note, unless you're parsing that JSON string by some other means or only expect users to have modern browers with a built in JSON parser you need to use a JS framework or JSON2 to parse the JSON string outputted by the server into a real JSON object.

// JSON received from server is in string format
var jsonString = '{"date":1251877601000}';

//use JSON2 or some JS library to parse the string
var jsonObject =  JSON.parse( jsonString );

//now you have your date!
alert( new Date(jsonObject.date) );

Wiki Link

Modern browsers, such as Firefox 3.5 and Internet Explorer 8, include special features for parsing JSON. As native browser support is more efficient and secure than eval(), it is expected that native JSON support will be included in the next ECMAScript standard.[6]


Link to JSON2 file

Live Example

查看更多
听够珍惜
6楼-- · 2019-01-01 09:35
//
// formats a .net date into a javascript compatible date
//
function FormatJsonDate(jsonDt) 
{              
    var MIN_DATE = -62135578800000; // const

    var date = new Date(parseInt(jsonDt.substr(6, jsonDt.length-8)));                                                       
    return date.toString() == new Date(MIN_DATE).toString() ? "" : (date.getMonth() + 1) + "\\" + date.getDate() + "\\" + date.getFullYear(); 
}
查看更多
十年一品温如言
7楼-- · 2019-01-01 09:38

What's wrong with:

new Date(1293034567877);

This returns for me "Wed Dec 22 2010 16:16:07 GMT+0000 (GMT Standard Time)".

Or do you need to get the number out the json?

查看更多
登录 后发表回答