python - 使用Python解码JSON,导出jqGrid时出错

标签 python django jqgrid json

我正在尝试将 jqGrid 导出到 CSV。到目前为止,我在 jqGrid 的标题中添加了一个按钮,该按钮运行一个 javascript 函数,该函数定向到带有变量的 get 请求的 CSV 下载 url:1) JSON 所在的 url,2) 来自 jqGrid 的 urlencoded header 。

我确实这样做了(“grid”是要下载的网格的名称):

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 文件的 View 中,我尝试使用 simplejson 读取一些 json,但收到错误:

JSONDecodeError at /csv/download/

No JSON object could be decoded: line 1 column 0 (char 0)

我使用的是 python 2.7.1 和 simplejson 2.6.2,simplejson 的回溯到第 426 行。

View 如下所示:

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 示例:

{"records": "0", "total": "1", "rows": [], "page": "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"}

最佳答案

这里有一个更好的方法。无需使用 urllib2 打开新连接,您需要的一切都在 django 内。

from django.core.urlresolvers import resolve
view_match = resolve('/json/test_day/4982/')
json_data = view_match.func(request,**view_match.kwargs).content

关于python - 使用Python解码JSON,导出jqGrid时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13898332/

相关文章:

python:更改符号变量并分配数值

python - 手动将 OPML 订阅(文件)导入到 Google Reader

python - 将 pandas 提取正则表达式与多个组一起使用

django - 我应该如何在我的模型中使用 DurationField?

python - Django、Postgres - 列无法自动转换为整数类型

python - 如何使用 Pandas 转置行和列?

python - 如何使用 django import-export 导入 excel 文件

javascript - 如何在 jqGrid 中仅显示水平网格线?

php - 使用 JQgrid 和 sortableRows。更新表行顺序

jquery - jqGrid - 网格列的 JSON 响应中的 "editable":"true"