我有一个小型 dancer 应用程序,它提供一些 HTML(包括调用 google charts API 的 javascript)和其他 URL 查询数据库,并以可以传递给 google.visualization 的形式返回编码 JSON 格式的数据。数据表。 javascript 在 dancer 应用程序中查询 JSON 数据,然后将其传递到 google charts 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>
问题是 Perl 返回的一些数据包括日期/时间戳,因此应该将类型设置为“datetime”:
{"rows":[{"c":[{"v":"WHAT_CAN_I_PUT_HERE"},{"v":"2095"}]}],"cols":[{"type":"datetime","label":"DTU"},{"type":"number","label":"COUNT"}]}
在 Javascript 中,您可以创建一个日期以传递给 google charts API:
new Date(2012, 1, 08, 09, 32, 0)
我如何从 Perl 发送以 JSON 编码的日期,以便 google charts API 能够理解它?如果您不能,我还有哪些其他选择?
最佳答案
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.
关于javascript - 从 Perl 发送 JSON 日期到 google charts API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9379124/