Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将JSON对象从requests.get()转换为dict_Python_Json_Mongodb_Python 2.7_Python Requests - Fatal编程技术网

Python 将JSON对象从requests.get()转换为dict

Python 将JSON对象从requests.get()转换为dict,python,json,mongodb,python-2.7,python-requests,Python,Json,Mongodb,Python 2.7,Python Requests,我正在从url中提取json对象: import requests r = requests.get('http://my.endpoint.com/Hardware.json') 现在我需要将它从r.json()转换为dict,这样我就可以将它作为文档插入MongoDB(我想,我是Mongo的新手) 当我尝试时: import json d = json.loads(r.json()) 它失败于: TypeError:应为字符串或缓冲区 最后,我只需要一个对象类型,我可以将它作为新文档从p

我正在从url中提取json对象:

import requests
r = requests.get('http://my.endpoint.com/Hardware.json')
现在我需要将它从
r.json()
转换为
dict
,这样我就可以将它作为文档插入MongoDB(我想,我是Mongo的新手)

当我尝试时:

import json
d = json.loads(r.json())
它失败于:

TypeError:应为字符串或缓冲区

最后,我只需要一个对象类型,我可以将它作为新文档从
pymongo
r.json()传递到
MongoClient().test.hosts.insert_one()
作为一个新文档,它可以返回一个
dict
列表,因此无需将其转换为json


如果它是一个dict,那么
insert\u one()
应该可以正常工作,但是如果它是一个列表,那么你需要使用
insert\u many()
方法。

r.json()已经是一个dict。没有必要转换它,那么为什么我得到
TypeError:document必须是dict的一个实例,bson.son.son…
当我尝试从pymongo将
r.json()
传递到
insert_one()
中时,您可以打印类型(r.json())?
,然后您就有了一个列表。如果要插入文档列表,必须使用pymongo中的insert_many()方法