如何在 django 中返回这个 JSON 而不会被转义?
Posted
技术标签:
【中文标题】如何在 django 中返回这个 JSON 而不会被转义?【英文标题】:How do I return this JSON in django without it being escaped? 【发布时间】:2018-03-08 14:47:39 【问题描述】:在我的 Django 项目中,我试图返回 JsonResponse
,但返回的数据正在“某处”转义。当我通过 Jupyter Notebook 运行代码时,我没有问题。我的 DataFrame 结构是:
我的 Django 响应读入我的 DataFrame pickle 并像这样处理它:
def API_FTEs_month(request, storeCode):
df1=pd.read_pickle(storeCode+'.pickle')
result=(df1.groupby(['Date','Job Role'], as_index=False)
.apply(lambda i: i[['Department', 'Team', 'Days']].to_dict('r'))
.reset_index()
.rename(columns=0: 'Assignments')
.to_json(orient='records'))
return JsonResponse(result, safe=False)
我不知道为什么,但是响应被这样转义了:
"[\"Date\":\"2017-12-31\",\"Job Role\":\"Junior\",\"Assignments\":[\"Department\":\"Electronics\",\"Team\":\"A\",\"Days\":12.8311126233,\"Department\":\"Electronics\",\"Team\":\"B\",\"Days\":9.7797036952,\"Department\":\"Electronics\",\"Team\":\"C\",\"Days\":12.4532628859,\"Department\":\"Household\",\"Team\":\"A\",\"Days\":13.2005991473,\"Department\":\"Household\",\"Team\":\"B\",\"Days\":11.2217690247,\"Department\":\"Household\",\"Team\":\"C\",\"Days\":9.9799650502],\"Date\":\"2017-12-31\",\"Job Role\":\"Senior\",\"Assignments\":[\"Department\":\"Electronics\",\"Team\":\"A\",\"Days\":12.3088204188,\"Department\":\"Electronics\",\"Team\":\"B\",\"Days\":11.6027520428,\"Department\":\"Electronics\",\"Team\":\"C\",\"Days\":8.4242249342,\"Department\":\"Household\",\"Team\":\"A\",\"Days\":10.2680664459,\"Department\":\"Household\",\"Team\":\"B\",\"Days\":10.7355819544,\"Department\":\"Household\",\"Team\":\"C\",\"Days\":14.4751405746],\"Date\":\"2018-01-31\",\"Job Role\":\"Junior\",\"Assignments\":[\"Department\":\"Electronics\",\"Team\":\"A\",\"Days\":9.8390990646,\"Department\":\"Electronics\",\"Team\":\"B\",\"Days\":7.8840336082,\"Department\":\"Electronics\",\"Team\":\"C\",\"Days\":7.4098884623,\"Department\":\"Household\",\"Team\":\"A\",\"Days\":6.5804561812,\"Department\":\"Household\",\"Team\":\"B\",\"Days\":7.9109739164,\"Department\":\"Household\",\"Team\":\"C\",\"Days\":7.6766255979],\"Date\":\"2018-01-31\",\"Job Role\":\"Senior\",\"Assignments\":[\"Department\":\"Electronics\",\"Team\":\"A\",\"Days\":5.9779944185,\"Department\":\"Electronics\",\"Team\":\"B\",\"Days\":7.8300778676,\"Department\":\"Electronics\",\"Team\":\"C\",\"Days\":7.9050436379,\"Department\":\"Household\",\"Team\":\"A\",\"Days\":6.9225874658,\"Department\":\"Household\",\"Team\":\"B\",\"Days\":7.6001780124,\"Department\":\"Household\",\"Team\":\"C\",\"Days\":6.5897367619]]"
在 Jupyter Notebook 中重新创建尝试我似乎得到了有效的 JSON:
笔记本:
我假设这是我的 to_json()
或 JsonResponse
中的问题,但我已尝试将其他 JSON 尝试插入到我的 JsonResponse
中,例如以下内容,这给了我有效的 JSON(但不是所需的结构)无需转义:
def nested_dict():
return collections.defaultdict(nested_dict)
result=nested_dict()
for row in df4.itertuples():
result[row.Index[0]][row.Index[1]][row[1]][row[2]]['sales'] = row.Days
json.dumps(result)
【问题讨论】:
如果把to_json
改成to_dict
,效果如何?
JsonResponse
需要一个 Python 对象,该对象随后将被 JSON 编码。您已经在传递 JSON,从而生成 JSON 编码的 JSON。
天啊!谢谢你们!
【参考方案1】:
你需要改变:
.to_json(orient='records')
致to_dict
:
.to_dict(orient='records')
为了防止双重编码json。
这在Klaus D.的评论中有更好的解释:
JsonResponse 需要一个 Python 对象,然后该对象将被 JSON 编码。您已经传入 JSON,导致 JSON 编码的 JSON。
【讨论】:
以上是关于如何在 django 中返回这个 JSON 而不会被转义?的主要内容,如果未能解决你的问题,请参考以下文章
Django 使用自定义 SQL 而不是模型将 JSON 对象返回到模板
如何从Django数据库中的不同表中获取选定的数据并将json返回给Web客户端
Django rest 框架序列化程序返回一个列表而不是 json