【发布时间】:2015-12-24 12:40:09
【问题描述】:
我正在从以下网址获取 API:http://api.example.com/search/foo/bar
使用这个简单的代码。
import json
url = "http://api.example.com/search/foo/bar"
result = json.loads(url) # result is now a dict
print result['name']
但是,我收到了这个错误。
Traceback (most recent call last):
File "index.py", line 6, in <module>
result = json.loads(url);
File "/usr/lib64/python2.7/json/__init__.py", line 338, in loads
return _default_decoder.decode(s)
File "/usr/lib64/python2.7/json/decoder.py", line 365, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib64/python2.7/json/decoder.py", line 383, in raw_decode
raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded
【问题讨论】:
-
"http://api.example.com/search/foo/bar"是一个字符串,你要下载页面吗? -
@KevinGuan 不,一点也不。这是一个 JSON 响应,不是字符串。
-
@arbi-g11324115 URL 是一个字符串。您必须先下载该页面才能获取其 json 内容。这就是凯文告诉你的。他是对的。