Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python django中的json解析_Python_Json_Django - Fatal编程技术网

Python django中的json解析

Python django中的json解析,python,json,django,Python,Json,Django,我构建了一个webhook来接收jsonPOST。但是,我从json解码器收到一个错误。这是webhook中的view.py文件 import json from django.shortcuts import render from django.views.decorators.http import require_POST from django.views.decorators.csrf import csrf_exempt from django.http import HttpRe

我构建了一个webhook来接收json
POST
。但是,我从json解码器收到一个错误。这是webhook中的view.py文件

import json
from django.shortcuts import render
from django.views.decorators.http import require_POST
from django.views.decorators.csrf import csrf_exempt
from django.http import HttpResponse
from .models import UserText

# Create your views here.
@csrf_exempt
@require_POST
def webhookmb(request):
    usrtxt = json.loads(request.body)

    UserText.objects.create(
        id = usrtxt['id'],
        recipient = usrtxt['recipient'],
        originator = usrtxt['originator'],
        body = usrtxt['body'],
        createdDatetime = usrtxt['createdDatetime'],
    )

    # redirect to API.AI
    # TODO


    return HttpResponse(200)
这就是我收到的错误

2017-07-09T00:19:45.559146+00:00 app[web.1]: Internal Server Error: /webhookmb/
2017-07-09T00:19:45.559154+00:00 app[web.1]: Traceback (most recent call last):
2017-07-09T00:19:45.559155+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/django/core/handlers/exception.py", line 41, in inner
2017-07-09T00:19:45.559156+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/django/core/handlers/base.py", line 187, in _get_respon
se
2017-07-09T00:19:45.559157+00:00 app[web.1]:     response = self.process_exception_by_middleware(e, request)
2017-07-09T00:19:45.559155+00:00 app[web.1]:     response = get_response(request)
2017-07-09T00:19:45.559159+00:00 app[web.1]:     response = wrapped_callback(request, *callback_args, **callback_kwargs)
2017-07-09T00:19:45.559158+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/django/core/handlers/base.py", line 185, in _get_respon
se
2017-07-09T00:19:45.559160+00:00 app[web.1]:     return view_func(*args, **kwargs)
2017-07-09T00:19:45.559159+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_v
iew
2017-07-09T00:19:45.559161+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/django/views/decorators/http.py", line 40, in inner
2017-07-09T00:19:45.559161+00:00 app[web.1]:     return func(request, *args, **kwargs)
2017-07-09T00:19:45.559162+00:00 app[web.1]:   File "/app/webhookmb/views.py", line 12, in webhookmb
2017-07-09T00:19:45.559162+00:00 app[web.1]:     usrtxt = json.loads(request.body)
2017-07-09T00:19:45.559163+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/json/__init__.py", line 354, in loads
2017-07-09T00:19:45.559163+00:00 app[web.1]:     return _default_decoder.decode(s)
2017-07-09T00:19:45.559164+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/json/decoder.py", line 339, in decode
2017-07-09T00:19:45.559165+00:00 app[web.1]:     obj, end = self.raw_decode(s, idx=_w(s, 0).end())
2017-07-09T00:19:45.559165+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/json/decoder.py", line 357, in raw_decode
2017-07-09T00:19:45.559166+00:00 app[web.1]:     raise JSONDecodeError("Expecting value", s, err.value) from None
2017-07-09T00:19:45.559170+00:00 app[web.1]: json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

我不太确定如何解决这个问题,我已经解决了很多问题,但是我被困在如何解决
json.decoder.JSONDecodeError:期望值:第1行第1列(char 0)
任何反馈都会令人惊讶。谢谢

这可能是由于编码不匹配

因为您没有提供json本身

试试这个

json.loads(request.body.decode('utf-8'))

可能是客户端没有向服务器发送正确的json吗?这是什么版本的django?也许您可以尝试打印request.POST、request.body等以查看发送的数据?类似的问题可能会有所帮助:。