ASP.NET MVC JsonResult日期格式 [英] ASP.NET MVC JsonResult Date Format

查看:517
本文介绍了ASP.NET MVC JsonResult日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个控制器动作,有效地只是返回我的模型的JsonResult。所以,在我的方法我有类似如下:

I have a controller action that effectively simply returns a JsonResult of my model. So, in my method I have something like the following:

return new JsonResult(myModel);

这工作得很好,除了一个问题。有一个在模型中的日期财产,这似乎在JSON结果像这样被退回:

This works well, except for one problem. There is a date property in the model and this appears to be returned in the Json result like so:

"\/Date(1239018869048)\/"

我应该如何处理日期打交道,使他们在我所需要的格式返回?或者,我怎么在脚本处理上面这种格式?

How should I be dealing with dates so they are returned in the format I require? Or how do I handle this format above in script?

推荐答案

只是为了扩大<一个href=\"http://stackoverflow.com/questions/726334/asp-mvc-jsonresult-date-format/726417#726417\">casperOne's回答。

借助 JSON规范不占日期值。 MS不得不打个电话,他们选择的道路是利用了JavaScript的重新$ P $字符串psentation一个小窍门:字符串/是一样的\\ /,一个字串意愿从不的获得序列化为\\ /(甚至\\ /必须映射到\\\\ /)。

The JSON spec does not account for Date values. MS had to make a call, and the path they chose was to exploit a little trick in the javascript representation of strings: the string literal "/" is the same as "\/", and a string literal will never get serialized to "\/" (even "\/" must be mapped to "\\/").

请参阅http://msdn.microsoft.com/en-us/library/bb299886.aspx#intro_to_json_topic2为更好的解释(向下滚动到从字面的JavaScript到JSON)

See http://msdn.microsoft.com/en-us/library/bb299886.aspx#intro_to_json_topic2 for a better explanation (scroll down to "From JavaScript Literals to JSON")

一个JSON的痛点是
  缺少日期/时间文字。许多
  人们惊讶和失望
  当他们第一次学习这种
  遇到JSON。简单的解释
  (安慰或不)为没有
  日期/时间文本是的JavaScript
  从未有过的事情:在支持
  日期和时间值在JavaScript是
  通过日期完全提供
  目的。使用JSON大多数应用程序
  作为数据格式,因此,一般
  往往使用一个字符串或
  号码前preSS日期和时间
  值。如果使用字符串,可以
  通常希望它是在ISO
  8601格式。如果一个数字被使用,
  相反,则该值通常为
  考虑意指数
  环球协调毫秒
  时间(UTC)时代以来,这里就是时代
  定义为午夜1970年1月1日
  (世界标准时间)。同样,这仅仅是一种
  在JSON的惯例,而不是部分
  标准。如果您的数据交换
  与其他应用程序,你会
  需要检查它的文档来查看
  如何连接codeS日期和时间值
  一个JSON中文字。例如,
  微软的ASP.NET AJAX既不使用
  所描述的约定。相反,
  它连接codeS .NET DateTime值作为
  JSON字符串,对其中内容
  字符串/日期(蜱)/在哪里
  蜱重新presents以来的毫秒
  纪元(UTC)。因此1989年11月29日,
  上午04时55分三十秒,在UTC是EN $ C $光盘作为
  \\ /日期(628318530718)\\ /。

One of the sore points of JSON is the lack of a date/time literal. Many people are surprised and disappointed to learn this when they first encounter JSON. The simple explanation (consoling or not) for the absence of a date/time literal is that JavaScript never had one either: The support for date and time values in JavaScript is entirely provided through the Date object. Most applications using JSON as a data format, therefore, generally tend to use either a string or a number to express date and time values. If a string is used, you can generally expect it to be in the ISO 8601 format. If a number is used, instead, then the value is usually taken to mean the number of milliseconds in Universal Coordinated Time (UTC) since epoch, where epoch is defined as midnight January 1, 1970 (UTC). Again, this is a mere convention and not part of the JSON standard. If you are exchanging data with another application, you will need to check its documentation to see how it encodes date and time values within a JSON literal. For example, Microsoft's ASP.NET AJAX uses neither of the described conventions. Rather, it encodes .NET DateTime values as a JSON string, where the content of the string is /Date(ticks)/ and where ticks represents milliseconds since epoch (UTC). So November 29, 1989, 4:55:30 AM, in UTC is encoded as "\/Date(628318530718)\/".

一个解决方案是只解析出来:

A solution would be to just parse it out:

value = new Date(parseInt(value.replace("/Date(", "").replace(")/",""), 10));

不过我听说有一个设置的地方,以获得串行输出的DateTime 新的日期(XXX)语法。我会尽力挖掘了这一点。

However I've heard that there is a setting somewhere to get the serializer to output DateTime objects with the new Date(xxx) syntax. I'll try to dig that out.

JSON.parse的()第二个参数接受齐磊函数,其中prescribes怎样的价值最初,产生了在返回之前。

The second parameter of JSON.parse() accepts a reviver function where prescribes how the value originally produced by, before being returned.

下面是日期的例子:

var parsed = JSON.parse(data, function(key, value) {
  if (typeof value === 'string') {
    var d = /\/Date\((\d*)\)\//.exec(value);
    return (d) ? new Date(+d[1]) : value;
  }
  return value;
});

见<一的文档href=\"https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse\">JSON.parse()

这篇关于ASP.NET MVC JsonResult日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆