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
在谷歌可视化API中是否有方法从JSON中检索日期值?这是操场上的鸟粪,请把下面的代码拷贝进去
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});
}
3 个解决方案
#1
9
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
我自己也遇到了这个问题,所以我想把答案粘贴到谷歌api文档中,它位于http://code.google.com/apis/chart/interactive/devs/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."
“JSON不支持JavaScript日期值(例如,“新日期(2008,1,28,0,31,26)”;API实现。但是,API现在支持自定义有效的日期JSON表示形式,格式如下:日期(年、月、日[、小时、分钟、秒、秒[、毫秒]),日之后的所有内容都是可选的,月是零基础的。
#2
1
I was running into same problem and above solution did not work. After searching for hours I found following post and the solution in there worked.
我遇到了同样的问题,上面的解决方案不起作用。在搜索了几个小时后,我找到了下面的帖子,其中的解决方案奏效了。
https://groups.google.com/forum/#!msg/google-visualization-api/SCDuNjuo7xo/ofAOTVbZg7YJ
https://groups.google.com/forum/ !味精/ google-visualization-api / SCDuNjuo7xo / ofAOTVbZg7YJ
Do not include "new" in the json string so it will be just: v:"Date(2009, 9, 28)"
不要在json字符串中包含“new”,因此它将是:v:“日期(2009年,9月,28日)”
#3
0
I suppose that the quote is not at the correct place in your snippet "new Date(2010, 3, 28")
Write instead "new Date(2010, 3, 28)"
我想在你的代码片段“新日期(2010年,3月,28日)”中引用的不是正确的位置,而是“新日期(2010年,3月,28日)”
Json format does not accept javascript object so the server return a string. JSON knows only numbers, boolean constants, string, null, vector and 'object' (much more a dictionnary).
Json格式不接受javascript对象,因此服务器返回一个字符串。JSON只知道数字、布尔常数、字符串、null、向量和'object'(更确切地说是字典)。
I suppose that you have to perform an eval() of the returned string (do not forget to check inputs).
我认为您必须执行返回的字符串的eval()(不要忘记检查输入)。
Another alternative is to use a Regex to extract the fields something like /new Date\((\d+),(\d+),(\d+)\)/
will work.
另一种选择是使用Regex提取一些字段,比如/new Date\((\d+)、(\d+)、(\d+)\ /将会工作。
#1
9
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
我自己也遇到了这个问题,所以我想把答案粘贴到谷歌api文档中,它位于http://code.google.com/apis/chart/interactive/devs/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."
“JSON不支持JavaScript日期值(例如,“新日期(2008,1,28,0,31,26)”;API实现。但是,API现在支持自定义有效的日期JSON表示形式,格式如下:日期(年、月、日[、小时、分钟、秒、秒[、毫秒]),日之后的所有内容都是可选的,月是零基础的。
#2
1
I was running into same problem and above solution did not work. After searching for hours I found following post and the solution in there worked.
我遇到了同样的问题,上面的解决方案不起作用。在搜索了几个小时后,我找到了下面的帖子,其中的解决方案奏效了。
https://groups.google.com/forum/#!msg/google-visualization-api/SCDuNjuo7xo/ofAOTVbZg7YJ
https://groups.google.com/forum/ !味精/ google-visualization-api / SCDuNjuo7xo / ofAOTVbZg7YJ
Do not include "new" in the json string so it will be just: v:"Date(2009, 9, 28)"
不要在json字符串中包含“new”,因此它将是:v:“日期(2009年,9月,28日)”
#3
0
I suppose that the quote is not at the correct place in your snippet "new Date(2010, 3, 28")
Write instead "new Date(2010, 3, 28)"
我想在你的代码片段“新日期(2010年,3月,28日)”中引用的不是正确的位置,而是“新日期(2010年,3月,28日)”
Json format does not accept javascript object so the server return a string. JSON knows only numbers, boolean constants, string, null, vector and 'object' (much more a dictionnary).
Json格式不接受javascript对象,因此服务器返回一个字符串。JSON只知道数字、布尔常数、字符串、null、向量和'object'(更确切地说是字典)。
I suppose that you have to perform an eval() of the returned string (do not forget to check inputs).
我认为您必须执行返回的字符串的eval()(不要忘记检查输入)。
Another alternative is to use a Regex to extract the fields something like /new Date\((\d+),(\d+),(\d+)\)/
will work.
另一种选择是使用Regex提取一些字段,比如/new Date\((\d+)、(\d+)、(\d+)\ /将会工作。