如何在 json 中输出 javascript 日期

发布于 2024-09-08 06:46:00 字数 1221 浏览 0 评论 0原文

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

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'
               }
            ]
         }
      ]
   }
}

如何输出 Date 函数而不将其包装在“Date()”等字符串分隔符中。

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.

Google wants the data as something like this:

    {
   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()'.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

命比纸薄 2024-09-15 06:46:00

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

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

并删除了这些 %% 字符及其旁边的字符串分隔符。

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

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);
月寒剑心 2024-09-15 06:46:00

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

不过,如果您可以控制生成提要并使用它的代码,您可能需要执行以下操作之一。首先,将日期作为时间戳传递。这很简单:

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

加载 JSON 后,您的代码将解析 datetime

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

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

我看到这个问题的另一种方法是使用特殊的字符串文字,它向您的脚本表明它是一个日期时间。它可以是值:

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

也可以是名称:

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

@ 只是充当代码的标志,表明该值需要某种后处理。两者都会通过验证。

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

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).

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:

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:

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.

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.

扮仙女 2024-09-15 06:46:00
    $data = preg_replace('@new Date\(([^\)]*)\)@', '"$1"', $data);
    $data = json_decode($data, true);

结果:

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

    $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"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文