如何将字典保存为pymongo中另一个字典的值为烧瓶?

时间:2016-10-16 07:08:46

标签: python dictionary pymongo

我有一个Json请求,如下所示:

{"name":"jane", "family": "doe",
"address":{"country":"Iran", "State": "Ilam", "city": "ilam"},
"age": "25" }

我可以使用以下方法将值输入变量:

name = request.json['name']
family = requst.json['family']
age = requst.json['age']

但是,如何获取地址字段并将其保存到变量?

1 个答案:

答案 0 :(得分:3)

如果您有以下字典,'address'是嵌套在另一个字典中的字典:

{"name":"jane", "family": "doe",
"address":{"country":"Iran", "State": "Ilam", "city": "ilam"},
"age": "25" }

以下列方式提取地址:

address = request.json['address']

>>> address
{'country': 'Iran', 'State': 'Ilam', 'city': 'ilam'}

您解压缩的地址现在是一个新字典,您需要从中提取值:

state = address['State']
city = address['city']
country = address['country']