简体   繁体   English

如何在pymongo中为flask保存一个字典作为另一个字典的值?

[英]How to save a dictionary as a value of another dictionary in pymongo for flask?

I have a Json request that looks like this: 我有一个Json请求,看起来像这样:

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

and i can get the values into a variable using: 我可以使用以下方法将值放入变量中:

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

but, how do i get the address field and save it to a variable? 但是,如何获取地址字段并将其保存到变量?

If you have the following dictionary, 'address' is a dictionary that is nested in another dictionary: 如果您具有以下字典,则“ address”是嵌套在另一个字典中的字典:

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

Extracting the address is done in the following way: 提取地址的方法如下:

address = request.json['address']

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

Address that you extracted is now a new dictionary, and you need to extract values from it like this: 现在,您提取的地址是一个新的字典,您需要像这样从中提取值:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM