Python 防止Django';s JsonResponse来序列化我的字符串
我有一个JSON字符串,我从一个数据帧计算Python 防止Django';s JsonResponse来序列化我的字符串,python,json,django,Python,Json,Django,我有一个JSON字符串,我从一个数据帧计算 aggr.aggregated.to_json(orient='values') 我不能直接向标准Python JSON序列化程序提供aggr.aggregated,因为它不会遵循orient='values'规则,并且会以不同的方式执行 我想将自己的JSON字符串作为Django视图的响应: return JsonResponse(aggr.aggregated.to_json(orient='values')) 然而,在上面的代码中,Djang
aggr.aggregated.to_json(orient='values')
我不能直接向标准Python JSON序列化程序提供aggr.aggregated
,因为它不会遵循orient='values'
规则,并且会以不同的方式执行
我想将自己的JSON字符串作为Django视图的响应:
return JsonResponse(aggr.aggregated.to_json(orient='values'))
然而,在上面的代码中,Django试图序列化我的JSON字符串
如何专门使用
JsonResponse
将内容类型头设置为application/json,而not来序列化已经序列化的字符串?如果您不想让JsonResponse
为您编码json,那么使用JsonResponse没有任何好处
只需使用HttpResponse
并自行设置内容类型标题:
return HttpResponse(
aggr.aggregated.to_json(orient='values'),
content_type='application/json'
)
如果您不想让它为您编码JSON,那么使用JsonResponse
没有任何好处
只需使用HttpResponse
并自行设置内容类型标题:
return HttpResponse(
aggr.aggregated.to_json(orient='values'),
content_type='application/json'
)