【发布时间】:2012-08-15 12:51:04
【问题描述】:
我在我的 PHP 代码中生成一个 JSON 文件(通过运行数据库查询)。这个 JSON 文件是使用 Google Chart Tools 的 Data Queries example 在我的 Javascript 客户端下载的:
function initialize() {
// Replace the data source URL on next line with your data source URL.
// Specify that we want to use the XmlHttpRequest object to make the query.
var opts = {sendMethod: 'xhr'};
var query = new google.visualization.Query('http://spreadsheets.google.com?key=123AB&...', opts);
// Optional request to return only column C and the sum of column B, grouped by C members.
query.setQuery('select C, sum(B) group by C');
// Send the query with a callback function.
query.send(handleQueryResponse);
}
function handleQueryResponse(response) {
if (response.isError()) {
alert('Error in query: ' + response.getMessage() + ' ' + response.getDetailedMessage());
return;
}
var data = response.getDataTable();
var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
chart.draw(data, {width: 400, height: 240, is3D: true});
}
JSON 文件相当大。在 PHP 中压缩 JSONstring 并在 Javascript 中解压缩的最佳方法是什么?
【问题讨论】:
-
查看 GZIP 压缩您的内容。如果您的服务器设置正确,它可以压缩任何
application/json内容,然后您的浏览器“应该”自动解压缩它。 bearpanther.com/2012/04/11/gzip-json-generated-on-the-fly -
感谢您的回答。它有效。
标签: php javascript json google-api google-visualization