我正在尝试使用simplejson来解析JSON字符串。出于某种原因,当我使用simplejson.loads时,我收到以下错误:
ERROR:root:Exception in request:
Traceback (most recent call last):
File "C:\django\lokus_web_new\django\core\handlers\base.py", line 111, in get_
response
response = callback(request, *callback_args, **callback_kwargs)
File "C:\django\lokus_web_new\mobile\views.py", line 13, in entry_ajax
test = simplejson.loads(entry_param)
File "C:\Program Files (x86)\Google\google_appengine\lib\simplejson\simplejson
\__init__.py", line 388, in loads
return _default_decoder.decode(s)
File "C:\Program Files (x86)\Google\google_appengine\lib\simplejson\simplejson
\decoder.py", line 402, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Program Files (x86)\Google\google_appengine\lib\simplejson\simplejson
\decoder.py", line 420, in raw_decode
raise JSONDecodeError("No JSON object could be decoded", s, idx)
JSONDecodeError: No JSON object could be decoded: line 1 column 0 (char 0)
INFO:root:"POST /entry_ajax/ HTTP/1.1" 500 -
JSON字符串由本机javscript JSON.stringify函数生成,如下所示:
{'type':'basic','id':'156','payload':{'text':'asd'}}
以下是视图的代码:
def entry_ajax(request):
entry_param = str(request.POST)
test = simplejson.loads(entry_param)
return HttpResponse(test['type'])
有什么想法吗?
答案 0 :(得分:2)
这不是有效的JSON字符串。使用双引号:
{"type":"basic","id":"156","payload":{"text":"asd"}}