django-關於JSON編碼格式的異常處理

這個是編碼格式的異常,由於在爬蟲元服務的對接中,經過http發送數據的時候,打包下來的數據封裝的格式不對python

string={'domain': 'android.myapp.com', 'url': 'https://android.myapp.com/myapp/searchAjax.htm?kw=MM%E5%BA%94%E7%94%A8%E5%95%86%E5%9C%BA&pns=&sid='}

由於JSON中並無單引號,僅有雙引號,因此咱們須要把單引號替換成雙引號了android

json.decoder.JSONDecodeError: Expecting property name enclosed in double quotesdjango

方式有多種,好比用正則json

data = re.sub('\'', '\"', string.decode("utf-8"))

還能夠同簡單的字符串處理app

string.replace('\"', '\'')

如下是系統的異常拋出dom

Traceback (most recent call last):
  File "/home/yerikyu/spidermax/lib/python3.5/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/home/yerikyu/spidermax/lib/python3.5/site-packages/django/core/handlers/base.py", line 126, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/yerikyu/spidermax/lib/python3.5/site-packages/django/core/handlers/base.py", line 124, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/yerikyu/spidermax/lib/python3.5/site-packages/django/views/generic/base.py", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "/home/yerikyu/spidermax/lib/python3.5/site-packages/django/views/generic/base.py", line 88, in dispatch
    return handler(request, *args, **kwargs)
  File "/home/yerikyu/SG/spidermax/spider/views.py", line 43, in post
    data = json.loads(request.body.decode("utf-8"))
  File "/usr/lib/python3.5/json/__init__.py", line 319, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.5/json/decoder.py", line 339, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python3.5/json/decoder.py", line 355, in raw_decode
    obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
相關文章
相關標籤/搜索