有没有一种方法可以从Google Visualization API中的JSON中检索日期值?
这是playground的代码段,请将下面的代码复制到其中
运行代码时,您将不会有任何结果。您应该从标有注释的日期值中删除引号,以便检索结果。
function drawVisualization() {
var JSONObject = {
cols:
[
{id: 'header1', label: 'Header1', type: 'string'},
{id: 'header2', label: 'Header2', type: 'date'}
],
rows:
[
{
c:
[
{v: 'Value1'},
{v: "new Date(2010, 3, 28)"} // <= This is the format I receive from WebService
]
},
{
c:
[
{v: 'Value2'},
{v: new Date(2010, 3, 28)} // <=This is the format Google API accepts
]
}
]
};
var data = new google.visualization.DataTable(JSONObject, 0.5);
visualization = new google.visualization.Table(document.getElementById('table'));
visualization.draw(data, {'allowHtml': true});
}
最佳答案
我本人只是遇到了这个问题,所以我想我将从google api文档(位于http://code.google.com/apis/chart/interactive/docs/dev/implementing_data_source.html#jsondatatable)中粘贴答案
“JSON不支持JavaScript日期值(例如,“new Date(2008,1,28,0,31,26)”);该API实现支持。但是,API现在支持将日期的自定义有效JSON表示形式为一个具有以下格式的字符串:Date(年,月,日[,小时,分钟,秒[,毫秒]]),其中日后的所有内容都是可选的,而月是从零开始的。”