Python <class 'requests.models.Response'> 到 Json
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25016301/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
<class 'requests.models.Response'> to Json
提问by Morgan Allen
I've never done any object oriented programming, only basic script writing.
我从来没有做过任何面向对象的编程,只有基本的脚本编写。
I'm playing around with grequests
我在玩 grequests
rs = (grequests.get('https://api.github.com/repositories?since='+str(page), auth=(login, password)) for page in pages)
blah = grequests.map(rs)
print type(blah[0])
The response is:
回应是:
<class 'requests.models.Response'>
Normally I convert the response to text and then load it into json so I can parse it, but I can't do that with this response.
通常我将响应转换为文本,然后将其加载到 json 中,以便我可以解析它,但是我不能用这个响应来做到这一点。
I understand the concept of classes but haven't used them or know really what to do with that response.
我了解类的概念,但还没有使用它们,也不知道如何处理该响应。
Is there a way I can convert it to json?
有没有办法将其转换为json?
采纳答案by alecxe
blah[0]in your case is a requests.models.Responseclass which, according to the source codeand the documentation, has json()method that deserializes the JSON response into a Python object using json.loads():
blah[0]在您的情况下是一个requests.models.Response类,根据源代码和文档,该类具有使用以下json()方法将 JSON 响应反序列化为 Python 对象的方法json.loads():
print blah[0].json()

