1

PHP コードで (データベース クエリを実行して) JSON ファイルを生成します。この JSON ファイルは、Google チャート ツールのデータ クエリの例を使用して、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で解凍する最良の方法は何ですか?

4

2 に答える 2

0

問題は「PHPでJSONを圧縮する方法」です。答えは使用ob_gzhandlerです。サーバー構成にアクセスできない場合にのみ、この方法を使用してください。

PHP コードを記述してすべての json リソースで gzip を取得する必要がない、次のソリューションをお勧めします。

サーバー構成を変更して、ネゴシエートされたコンテンツの自動 gzip を許可します

アパッチ .htaccess

`AddOutputFilterByType DEFLATE text/html ... application/json`

Nginx server.conf

gzip             on; // if not already set
gzip_comp_level  9;
gzip_types       application/json;

より多くの gzip_types を使用した例:

gzip_types text/text text/html text/plain text/xml 
           text/css application/x-javascript application/javascript 
           application/json;
于 2014-04-06T12:13:32.940 に答える
0

@Gavin は上記のコメントで正しい答えを出しました:
彼の答えは: コンテンツの GZIP を調べてください。サーバーが正しくセットアップされている場合、アプリケーション/json コンテンツを圧縮でき、ブラウザーは自動的に解凍する必要があります。http://bearpanther.com/2012/04/11/gzip-json-generated-on-the-fly

于 2012-10-30T12:12:38.730 に答える