jqGridをCSVにエクスポートしようとしています。これまでのところ、jqGridのキャプションにボタンを追加しました。このボタンは、変数を持つgetリクエストを使用してCSVダウンロードURLに送信するjavascript関数を実行します。1)JSONが配置されているURLと2)jqGridのurlencodedヘッダー。
私がこのようにしたこと(「グリッド」はダウンロードされるグリッドの名前です):
function downloadGrid(grid) {
var columnNames = $(grid).getGridParam("colNames");
columnNames = encodeURIComponent(columnNames)
var dataLoc = $(grid).getGridParam("url");
window.open( "/csv/download/?header=" + columnNames+"&jqgrid=" + dataLoc);
}
CSVファイルを書き込むためのビューで、simplejsonを使用していくつかのjsonを読み込もうとしていますが、エラーが発生します。
JSONDecodeError at /csv/download/
No JSON object could be decoded: line 1 column 0 (char 0)
私はpython2.7.1とsimplejson2.6.2を使用していますが、simplejsonへのトレースバックは426行目にあります。
ビューは次のようになります。
import simplejson as json
import csv
import urllib2
from django.http import HttpResponse
from settings import PRIMARY_DOMAIN
def csv_writer(request):
response = HttpResponse(mimetype='text/csv')
dat = '%s' % datetime.now()
dat = dat[0:16]
response['Content-Disposition'] = 'attachment; filename="CSV_%s.csv"' % dat
writer = csv.writer(response)
json_data = urllib2.urlopen(PRIMARY_DOMAIN + '/json/test_day/4982/')
if request.method == "GET":
if 'header' in request.GET.keys():
header = request.GET['header'].split(',')
writer.writerow([str(x) for x in header])
if 'jqgrid' in request.GET.keys():
url = request.GET['jqgrid']
json_data = urllib2.urlopen(PRIMARY_DOMAIN + url)
data = json.loads(json_data.read())
###below here may not work, haven't gotten past the json.loads()
for row in data:
writer.writerow(row)
return response
これが私にとって失敗したjsonの2つの例です:
{"records": "0", "total": "1", "rows": [], "page": "1"}
もう1つは:
{"records": "17", "total": "1", "rows": [{"cell": ["04/05/10", 4, 196, 73, 3.0, 3.6, 1.5, 0.83, 8.0, 67, 28452, "", 115, 3.2, "$20.76", "$15.16"], "id": 1}, {"cell": ["01/30/10", 4, 131, 75, 4.0, 3.0, null, 1.33, null, 81, null, "", 141, 3.5, "$18.34", "$13.75"], "id": 2}, {"cell": ["01/06/10", 4, 107, 114, 3.3, 3.0, null, 1.1, null, 110, null, "", 283, 4.5, "$17.11", "$19.50"], "id": 3}, {"cell": ["11/28/09", 4, 68, 105, 3.7, 2.8, null, 1.32, null, 108, null, "", 214, 4.1, "$17.30", "$18.16"], "id": 4}, {"cell": ["11/02/09", 4, 42, 99, 4.1, 2.5, null, 1.64, null, 108, null, "", 47, 1.9, "$17.40", "$17.23"], "id": 5}, {"cell": ["10/02/09", 4, 11, 94, 3.9, 3.2, null, 1.22, null, 100, null, "", 17, 0.4, "$19.29", "$18.13"], "id": 6}], "page": "1"}