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

查看:31
本文介绍了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?

推荐答案

只是为了扩展 casperOne 的答案.

JSON 规范 不考虑日期值.MS 不得不打电话,他们选择的路径是利用 javascript 字符串表示中的一个小技巧:字符串文字/"与/"相同,而字符串文字将永远 序列化为/"(甚至/"也必须映射到\/").

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 的应用程序作为一种数据格式,因此,通常倾向于使用字符串或表示日期和时间的数字值.如果使用字符串,您可以通常期望它在 ISO 中8601 格式.如果使用数字,相反,那么该值通常是用来表示数量通用协调中的毫秒自纪元以来的时间 (UTC),其中纪元是定义为 1970 年 1 月 1 日午夜(世界标准时间).再次,这只是一个约定而不是 JSON 的一部分标准.如果您正在交换数据使用另一个应用程序,您将需要检查其文档以查看它如何编码日期和时间值在 JSON 文字中.例如,Microsoft 的 ASP.NET AJAX 既不使用所描述的约定.相当,它将 .NET DateTime 值编码为JSON 字符串,其中的内容字符串是/Date(ticks)/和滴答表示自此以来的毫秒数纪元(UTC).所以 1989 年 11 月 29 日,4:55:30 AM,在 UTC 中被编码为"/日期(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));

但是我听说有一个设置可以让序列化程序使用 new Date(xxx) 语法输出 DateTime 对象.我会努力把它挖出来.

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() 的第二个参数接受一个 reviver 函数,该函数规定在返回之前最初产生的值是如何产生的.

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

查看JSON.parse()

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

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