javascript 将 JSON 日期从 Perl 发送到谷歌图表 API
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9379124/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
sending JSON dates to google charts API from Perl
提问by bohica
I have a small dancer application which serves up some HTML (including the javascript to call the google charts API) and for other URLs queries a database and returns the data in encoded JSON in a form you can pass to google.visualization.DataTable. The javascript queries the dancer app for the JSON data then passes it into the google charts API - A simplified version is:
我有一个小型的舞者应用程序,它提供一些 HTML(包括调用 google 图表 API 的 javascript)和其他 URL 查询数据库并以可以传递给 google.visualization.DataTable 的形式返回编码 JSON 中的数据。javascript 查询舞者应用程序以获取 JSON 数据,然后将其传递到谷歌图表 API - 简化版本是:
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
google.load('visualization', '1.0', {'packages':['corechart']});
google.setOnLoadCallback(initialize);
function initialize() {
var res = $.ajax({
url: "/data/2",
dataType:"json",
async: false,
data: "{}",
contentType: "application/json",
error: function(jqXHR, textStatus, errorThrown) {
alert('textStatus ' + textStatus);
alert('errorThrown ' + errorThrown);
}
});
jsonData = res.responseText;
var data = new google.visualization.DataTable(jsonData);
var chart = new google.visualization.BarChart(document.getElementById('chart_div'));
chart.draw(data, {width: 400, height: 240});
}
</script>
The problem is that some of the data returned by the Perl includes dates/timestamps and so should have the type set to "datetime":
问题是 Perl 返回的一些数据包括日期/时间戳,因此应该将类型设置为“datetime”:
{"rows":[{"c":[{"v":"WHAT_CAN_I_PUT_HERE"},{"v":"2095"}]}],"cols":[{"type":"datetime","label":"DTU"},{"type":"number","label":"COUNT"}]}
In Javascript you could create a date to pass to the google charts API with:
在 Javascript 中,您可以创建一个日期以传递给谷歌图表 API:
new Date(2012, 1, 08, 09, 32, 0)
How can I send a date encoded in JSON from Perl such that the google charts API understands it? If you cannot what other options might be available to me?
如何从 Perl 发送以 JSON 编码的日期,以便 google charts API 理解它?如果你不能,我还有什么其他选择?
回答by benjaoming
JSON does not support datetime as a data types. But according to Google's documentation, you can just send a string with this format:
JSON 不支持日期时间作为数据类型。但是根据 Google 的文档,您可以只发送具有以下格式的字符串:
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 日期值(例如,“new Date(2008,1,28,0,31,26)”;API 实现支持。但是,API 现在支持将日期的自定义有效 JSON 表示形式为以下格式的字符串:Date(year,month,day[,hour,minute,second[,millisecond]]) 其中 day 之后的所有内容都是可选的,并且月份是从零开始的。
回答by justaguy
just to clarify: you should write the cell value as a string that looks like that: Date(year, month). for your example:
只是为了澄清:您应该将单元格值写为如下所示的字符串:日期(年,月)。对于您的示例:
{"rows":[{"c":[{"v":"Date(year, month)"},{"v":"2095"}]}],"cols":[{"type":"datetime","label":"DTU"},{"type":"number","label":"COUNT"}]}
hope that helps, and correct for you as i'm using it in php and not perl
希望对您有所帮助,并为您纠正,因为我在 php 而不是 perl 中使用它
回答by Sohail
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/#!msg/google-visualization-api/SCDuNjuo7xo/ofAOTVbZg7YJ
Do not include "new" in the json string so it will be just: "Date(2012, 1, 08, 09, 32, 0)"
不要在 json 字符串中包含“new”,所以它只是:“Date(2012, 1, 08, 09, 32, 0)”