如何将Date值从JSON返回到Google Visualization API [英] How to return a Date value from JSON to Google Visualization API
问题描述
这是游乐场 的snipplet请复制下面的代码
当您运行代码时,结果中将不会有任何内容。您应该删除我用注释标记的日期值中的引号,以便检索结果。
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)} //< =这是我从WebService
收到的格式
},
{
c:
[
{v:'Value2'},
{v:new Date(2010,3,28)} / /< =这是Google API接受的格式
]
}
]
};
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表示形式格式:日期(年,月,日[,小时,分钟,秒[,毫秒]])其中一切都是可选的,几个月是零。
is there a way to retrieve date value from JSON in Google Visualization API? Here is the snipplet for playground please copy the code below into it
When you run the code you won't have anything in result. you should remove the quotes from the date value I marked with comment in order to retrieve result.
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});
}
I just ran into this problem myself, so I thought I'd paste the answer from the google api documentation, located here http://code.google.com/apis/chart/interactive/docs/dev/implementing_data_source.html#jsondatatable
"JSON does not support JavaScript Date values (for example, "new Date(2008,1,28,0,31,26)"; the API implementation does. However, the API does now support a custom valid JSON representation of dates as a string in the following format: Date(year, month, day[,hour, minute, second[, millisecond]]) where everything after day is optional, and months are zero-based."
这篇关于如何将Date值从JSON返回到Google Visualization API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!