3

我正在尝试使用 simplejson 来解析来自 ustream 数据 api 的请求,并且在解码时出现此错误。我是 python 中 json 库的新手,所以我不确定从哪里开始寻找解决方案。

>>> import simplejson as json
>>> import requests as requests
>>> r = requests.get("http://api.ustream.tv/json/stream/popular/search/all?key=y
ourDevKey")
>>> in_json = None
>>> json.loads(in_json)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python27\lib\site-packages\simplejson-2.3.0-py2.7.egg\simplejson\__in
it__.py", line 413, in loads
    return _default_decoder.decode(s)
  File "C:\Python27\lib\site-packages\simplejson-2.3.0-py2.7.egg\simplejson\deco
der.py", line 402, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
TypeError: expected string or buffer

有什么帮助吗?

4

1 回答 1

4

这样做时:

>>> in_json = None
>>> json.loads(in_json)

您要求 json 解析None...并且 None 不是 JSon 对象,这就是您的问题的原因。

我认为做这样的事情会更好

>>> r = requests.get("http://api.ustream.tv/json/stream/popular/search/all?key=y
ourDevKey")
>>> json.loads(r.content)
于 2011-12-07T08:16:44.720 回答