如何在 json 中输出 javascript 日期
我正在尝试时间线图表: 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我只是将函数包装在 %% 字符中,如下所示:
并删除了这些 %% 字符及其旁边的字符串分隔符。
I simply wrapped the functions in %% characters like so:
And removed these %% characters and the string delimiters next to them.
不幸的是,JavaScript 中没有“日期文字”(数组可以用
[]
表示,对象可以用{}
表示,但对 Date 对象却没有这样的喜爱)。同样,实际有效的 JSON 只接受原始值(如字符串、数字、数组、布尔值、对象等等)。您可能还会惊讶地发现 JSON 中的Date()
无效(尽管如果您不关心可移植性,这不是问题)。不过,如果您可以控制生成提要并使用它的代码,您可能需要执行以下操作之一。首先,将日期作为时间戳传递。这很简单:
加载 JSON 后,您的代码将解析
datetime
:在这里,您的代码必须期望
datetime
属性并知道对其进行解码。所以这不是一个很好的通用解决方案。我看到这个问题的另一种方法是使用特殊的字符串文字,它向您的脚本表明它是一个日期时间。它可以是值:
也可以是名称:
@
只是充当代码的标志,表明该值需要某种后处理。两者都会通过验证。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 theDate()
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:
After loading the JSON, your code would then parse the
datetime
with: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:
Or it could be name:
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.
结果:
Result: