将JSON日期从Perl发送到Google图表API [英] sending JSON dates to google charts API from Perl
问题描述
我有一个小型舞者应用程序,它提供一些HTML(包括javascript来调用google图表API),并为其他URL查询数据库并以可以传递给google.visualization的形式返回编码JSON中的数据。数据表。 javascript查询JSON数据的舞者应用程序,然后将其传递到谷歌图表API - 简化版本为:
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:
<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:
The problem is that some of the data returned by the Perl includes dates/timestamps and so should have the type set to "datetime":
{"rows":[{"c":[{"v":"WHAT_CAN_I_PUT_HERE"},{"v":"2095"}]}],"cols":[{"type":"datetime","label":"DTU"},{"type":"number","label":"COUNT"}]}
在Javascript中,您可以创建日期以传递给Google图表API:
In Javascript you could create a date to pass to the google charts API with:
new Date(2012, 1, 08, 09, 32, 0)
如何从Perl中发送以JSON编码的日期,以便Google图表API能够理解它?如果你不能什么其他选择可能会提供给我?
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?
推荐答案
JSON不支持日期时间的数据类型。但根据Google的文档,您只需发送一个字符串格式即可:
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不支持JavaScript日期值(例如新日期(2008,1,28,0,31,26); API实现,但API现在支持以下格式的日期的自定义有效JSON表示形式作为字符串:日期(年,月,每天[,小时,分钟,秒[,毫秒]]),其中一天之后的所有内容都是可选的,而且月份是基于零的。
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日期从Perl发送到Google图表API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!