相关疑难解决方法(0)

Python:当没有返回时处理JSON解码错误

我正在解析json数据.解析时我没有问题,我正在使用simplejson模块.但是一些api请求返回空值.这是我的例子:

{
"all" : {
    "count" : 0,
    "questions" : [     ]
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我解析json对象的代码片段:

 qByUser = byUsrUrlObj.read()
 qUserData = json.loads(qByUser).decode('utf-8')
 questionSubjs = qUserData["all"]["questions"]
Run Code Online (Sandbox Code Playgroud)

正如我提到的一些请求,我收到以下错误:

Traceback (most recent call last):
  File "YahooQueryData.py", line 164, in <module>
    qUserData = json.loads(qByUser)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/simplejson/__init__.py", line 385, in loads
    return _default_decoder.decode(s)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/simplejson/decoder.py", line 402, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/simplejson/decoder.py", line 420, in raw_decode
    raise JSONDecodeError("No JSON object could be decoded", s, idx)
simplejson.decoder.JSONDecodeError: No JSON object …
Run Code Online (Sandbox Code Playgroud)

python json python-3.x

57
推荐指数
2
解决办法
13万
查看次数

标签 统计

json ×1

python ×1

python-3.x ×1