如何在json中输出javascript日期

时间:2021-06-06 12:54:12

I am trying to the timeline chart: http://code.google.com/apis/visualization/documentation/gallery/annotatedtimeline.html#Data_Format Data is coming in the form of a JSON feed.

我正在尝试时间线图表:http://code.google.com/apis/visualization/documentation/gallery/annotatedtimeline.html#Data_Format数据以JSON Feed的形式出现。

Google wants the data as something like this:

Google希望数据如下:

    {
   version:'0.6',
   reqId:'0',
   status:'ok',
   sig:'4641982796834063168',
   table:{
      cols:[
         {
            id:'A',
            label:'NEW A',
            type:'string'
         },
         {
            id:'B',
            label:'B-label',
            type:'number'
         },
         {
            id:'C',
            label:'C-label',
            type:'datetime'
         }
      ],
      rows:[
         {
            c:[
               {
                  v:'c'
               },
               {
                  v:3.0,
                  f:'3'
               },
               {
                  v:new Date(2008,
                  3,
                  30,
                  0,
                  31,
                  26                  ),
                  f:'4/30/08 12:31 AM'
               }
            ]
         }
      ]
   }
}

How can I output the Date function without it being wrapped in string delimiters like 'Date()'.

如何输出Date函数而不将其包装在像'Date()'这样的字符串分隔符中。

3 个解决方案

#1


4  

I simply wrapped the functions in %% characters like so:

我只是将函数包装成%%字符,如下所示:

$something = array('%%new Date(...) %%','somevalue');
$json = json_encode($something);

And removed these %% characters and the string delimiters next to them.

并删除了这些%%字符和它们旁边的字符串分隔符。

$json = preg_replace("/(('|\")%%|%%(\"|'))/",'', $json);

#2


1  

Unfortunately, there is no "date literal" in JavaScript (arrays can be expressed with [] and objects with {}, but no such love for Date objects). As well, actual, valid JSON only accepts primitive values (like strings, numbers, arrays, booleans, objects and not much else). You may also be surprised to learn that the Date() in JSON is not valid (though that's not a problem if you don't care about portability).

遗憾的是,JavaScript中没有“日期文字”(数组可以用[]表示,而对象用{}表示,但对Date对象没有这样的喜爱)。同样,实际有效的JSON只接受原始值(如字符串,数字,数组,布尔值,对象等等)。您可能也会惊讶地发现JSON中的Date()无效(尽管如果您不关心可移植性,这不是问题)。

If you have control over the code that produces the feed and consumes it, though, you may want to do one of a couple things. First, pass the date as a timestamp. This is easy enough:

但是,如果您可以控制生成Feed的代码并使用它,那么您可能需要执行以下操作之一。首先,将日期作为时间戳传递。这很容易:

var dtDateTime = new Date('Jan 27 2011 00:00:00 GMT+0000');
var intDateTime = dtDateTime.getTime();
var objJSON = {
    "datetime":intDateTime
};

After loading the JSON, your code would then parse the datetime with:

加载JSON后,您的代码将解析日期时间:

var dtDateTime = new Date();
dtDateTime.setTime(objJson.datetime);

Here, your code would have to expect the datetime property and know to decode it. So it's not a great generalized solution.

在这里,您的代码必须期望datetime属性并知道解码它。所以这不是一个很好的通用解决方案。

Another way I've seen this nut cracked is with a special string literal, that signifies to your script that it is a datetime. It could be in the value:

我看到这个坚果破解的另一种方法是使用特殊的字符串文字,这表示你的脚本是一个日期时间。它可能在价值中:

var objJSON = {
    "datetime":"@Jan 27 2011 00:00:00GMT+0000@"
};

Or it could be name:

或者它可能是名称:

var objJSON = {
    "@datetime":1296086400000
};

The @ simply acts as a flag to your code that the value needs some sort of post-processing. Both will pass validation.

@只是作为代码的标志,值需要某种后处理。两者都将通过验证。

JSON is meant to be simple and cross-platform, so anything that is specific to JS is inherently bad. If you tried to load JSON into, say Java or C#, you'd have a problem.

JSON意味着简单且跨平台,因此任何特定于JS的东西本质上都是坏的。如果您尝试将JSON加载到Java或C#中,那么您就会遇到问题。

#3


0  

    $data = preg_replace('@new Date\(([^\)]*)\)@', '"$1"', $data);
    $data = json_decode($data, true);

Result:

new Date(2008,3,30,0,31,26) => "2008,3,30,0,31,26"

新日期(2008,3,30,0,31,26)=>“2008,3,30,0,31,26”

#1


4  

I simply wrapped the functions in %% characters like so:

我只是将函数包装成%%字符,如下所示:

$something = array('%%new Date(...) %%','somevalue');
$json = json_encode($something);

And removed these %% characters and the string delimiters next to them.

并删除了这些%%字符和它们旁边的字符串分隔符。

$json = preg_replace("/(('|\")%%|%%(\"|'))/",'', $json);

#2


1  

Unfortunately, there is no "date literal" in JavaScript (arrays can be expressed with [] and objects with {}, but no such love for Date objects). As well, actual, valid JSON only accepts primitive values (like strings, numbers, arrays, booleans, objects and not much else). You may also be surprised to learn that the Date() in JSON is not valid (though that's not a problem if you don't care about portability).

遗憾的是,JavaScript中没有“日期文字”(数组可以用[]表示,而对象用{}表示,但对Date对象没有这样的喜爱)。同样,实际有效的JSON只接受原始值(如字符串,数字,数组,布尔值,对象等等)。您可能也会惊讶地发现JSON中的Date()无效(尽管如果您不关心可移植性,这不是问题)。

If you have control over the code that produces the feed and consumes it, though, you may want to do one of a couple things. First, pass the date as a timestamp. This is easy enough:

但是,如果您可以控制生成Feed的代码并使用它,那么您可能需要执行以下操作之一。首先,将日期作为时间戳传递。这很容易:

var dtDateTime = new Date('Jan 27 2011 00:00:00 GMT+0000');
var intDateTime = dtDateTime.getTime();
var objJSON = {
    "datetime":intDateTime
};

After loading the JSON, your code would then parse the datetime with:

加载JSON后,您的代码将解析日期时间:

var dtDateTime = new Date();
dtDateTime.setTime(objJson.datetime);

Here, your code would have to expect the datetime property and know to decode it. So it's not a great generalized solution.

在这里,您的代码必须期望datetime属性并知道解码它。所以这不是一个很好的通用解决方案。

Another way I've seen this nut cracked is with a special string literal, that signifies to your script that it is a datetime. It could be in the value:

我看到这个坚果破解的另一种方法是使用特殊的字符串文字,这表示你的脚本是一个日期时间。它可能在价值中:

var objJSON = {
    "datetime":"@Jan 27 2011 00:00:00GMT+0000@"
};

Or it could be name:

或者它可能是名称:

var objJSON = {
    "@datetime":1296086400000
};

The @ simply acts as a flag to your code that the value needs some sort of post-processing. Both will pass validation.

@只是作为代码的标志,值需要某种后处理。两者都将通过验证。

JSON is meant to be simple and cross-platform, so anything that is specific to JS is inherently bad. If you tried to load JSON into, say Java or C#, you'd have a problem.

JSON意味着简单且跨平台,因此任何特定于JS的东西本质上都是坏的。如果您尝试将JSON加载到Java或C#中,那么您就会遇到问题。

#3


0  

    $data = preg_replace('@new Date\(([^\)]*)\)@', '"$1"', $data);
    $data = json_decode($data, true);

Result:

new Date(2008,3,30,0,31,26) => "2008,3,30,0,31,26"

新日期(2008,3,30,0,31,26)=>“2008,3,30,0,31,26”