python - 类型错误 : the JSON object must be str, 不是 'Response' 与 Python 3.4

标签 python json python-3.x raspberry-pi

我收到此错误,但不知道问题出在哪里:

Traceback (most recent call last):
  File "C:/Python34/Scripts/ddg.py", line 8, in <module>
    data = json.loads(r)
  File "C:\Python34\lib\json\__init__.py", line 312, in loads
    s.__class__.__name__))
TypeError: the JSON object must be str, not 'Response'

这是我的代码:
import requests, json

search_q = input('Enter query: ')

r = requests.get('https://api.duckduckgo.com/?q=%s&format=json&pretty=1' % search_q)

if r.status_code == requests.codes.ok:
    data = json.loads(r)
    node = data['RelatedTopics']['Result']
    print (str(node))
else:
    bad_r.raise_for_status()

最佳答案

无需手动加载 JSON;响应有 json方法。

data = r.json()

关于python - 类型错误 : the JSON object must be str, 不是 'Response' 与 Python 3.4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35291021/

相关文章:

java - Jackson 序列化继承和方法重载问题

python - 如何检查一个字符串是否至少包含2位数字且不包含空格?

python easy_install pylint错误: The system cannot find the file specified

python - 在 Pandas 中读取包含列表的 csv

python - 在没有 NAN 的情况下将 Pandas DF 转换为 JSON

Python - 仅获取连接的本地NIC的MAC地址

javascript - Mobx可观察深度对象

javascript - JavaScript 和/或 JSON 是否需要解析器按定义顺序枚举属性?

python - Sqlite3 INSERT查询错误?

python - 无法在shinyapps.io上运行python代码