python 3.6 JsonResponse 问题

Posted

技术标签:

【中文标题】python 3.6 JsonResponse 问题【英文标题】:python 3.6 JsonResponse issue 【发布时间】:2018-04-13 01:41:38 【问题描述】:

所以我最近迁移到 Python 3.6 和 Django 1.11,我的 JsonResponse 代码如下所示:

   return JsonResponse('status': '1') 

它工作正常,但迁移后我开始收到此错误:

TypeError: 'bytes' 类型的对象不是 JSON 可序列化的

打印传递给 JsonResponse 的数据类型后,我意识到 python 3.6 将其从 dict 更改为 byte。 所以我更改了代码以确保我传递了一个字典。

尝试所有这些后,我仍然遇到同样的错误:

    data = dict([('status', 0)])
    print(data)
    print(type(data))
    # print(type(json.dumps(data)))
    # data = "status": '0'
    # data = json.dumps(data)
    # json.dumps(data.decode("utf-8"))
    #response = json.JSONEncoder().encode("status": 0) 
    #JsonResponse(data, safe=False)
    # response = json.dumps(data)
    print(JsonResponse(data, safe=False))
    return JsonResponse(data, safe=False)

打印:

    'status': 0
    <class 'dict'>
    <JsonResponse status_code=200, "application/json">

使用 json.dumps 选项时会出现此错误

AttributeError: 'str' 对象没有属性 'get'

任何帮助将不胜感激

追溯

    Traceback (most recent call last):
    File "/Users/andresvillavicencio/bancompara.mx/lib/python3.6/site-packages/django/core/handlers/base.py", line 131, in get_response
    response = middleware_method(request, response)
    File "/Users/andresvillavicencio/bancompara.mx/lib/python3.6/site-packages/django/contrib/sessions/middleware.py", line 58, in process_response
request.session.save()
    File "/Users/andresvillavicencio/bancompara.mx/lib/python3.6/site-packages/django/contrib/sessions/backends/db.py", line 81, in save
return self.create()
    File "/Users/andresvillavicencio/bancompara.mx/lib/python3.6/site-packages/django/contrib/sessions/backends/db.py", line 54, in create
self.save(must_create=True)
    File "/Users/andresvillavicencio/bancompara.mx/lib/python3.6/site-packages/django/contrib/sessions/backends/db.py", line 83, in save
obj = self.create_model_instance(data)
    File "/Users/andresvillavicencio/bancompara.mx/lib/python3.6/site-packages/django/contrib/sessions/backends/db.py", line 69, in create_model_instance
session_data=self.encode(data),
    File "/Users/andresvillavicencio/bancompara.mx/lib/python3.6/site-packages/django/contrib/sessions/backends/base.py", line 98, in encode
serialized = self.serializer().dumps(session_dict)
    File "/Users/andresvillavicencio/bancompara.mx/lib/python3.6/site-packages/django/core/signing.py", line 93, in dumps
return json.dumps(obj, separators=(',', ':')).encode('latin-1')
    File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/__init__.py", line 238, in dumps
**kw).encode(obj)
    File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
    File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/encoder.py", line 257, in iterencode
return _iterencode(o, 0)
    File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/encoder.py", line 180, in default
o.__class__.__name__)
   TypeError: Object of type 'bytes' is not JSON serializable

【问题讨论】:

我添加了完整的回溯,我知道我不应该得到那个错误,我在使用函数之前打印了所有类型的对象并且字典和字符串都很好。这就是为什么如此令人愤怒@Alasdair 它适用于 python 3.5 吗?我认为这个问题只与 json 序列化程序有关。介意检查 py 2.7 吗? 回溯显示当 Django 尝试保存 Django 会话时发生错误。问题不是return JsonResponse('status': '1')。在此之前,您必须在视图中执行类似 request.session['my_key'] = b'bytes' 的操作。 是的,变量编码不正确,谢谢@Alasdair 【参考方案1】:

问题不在于return JsonResponse('status': '1')

回溯显示当 Django 尝试保存 Django 会话时发生错误。

你必须在视图中做这样的事情:

request.session['my_key'] = b'bytes'

例如,您必须解码字节对象(或改用字符串):

request.session['my_key'] = b'bytes'.decode('utf-8')

【讨论】:

你是真正的英雄!

以上是关于python 3.6 JsonResponse 问题的主要内容,如果未能解决你的问题,请参考以下文章

python测试开发django-16.JsonResponse返回中文编码问题

django-16.JsonResponse返回中文编码问题

JsonResponse对象浅析

python 3.6 setup

Django 之 JsonResponse 对象

JsonResponse | Django开发