TypeError:JSON对象必须为str,而不是Python 3.4中的"Response" [英] TypeError: the JSON object must be str, not 'Response' with Python 3.4

查看:151
本文介绍了TypeError:JSON对象必须为str,而不是Python 3.4中的"Response"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到此错误,但我不知道是什么问题:

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方法.

No need to load the JSON manually; the response has a json method.

data = r.json()

这篇关于TypeError:JSON对象必须为str,而不是Python 3.4中的"Response"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆