【问题标题】:How to save a dictionary as a value of another dictionary in pymongo for flask?如何将字典保存为 pymongo for flask 中另一个字典的值?
【发布时间】:2017-02-25 08:10:59
【问题描述】:

我有一个如下所示的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']

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

【问题讨论】:

  • 我不明白你在问什么。您刚刚演示了如何访问字典中的数据。有什么困难?
  • 如何获取变量中的地址字段?

标签: python dictionary pymongo


【解决方案1】:

如果您有以下字典,“地址”是嵌套在另一个字典中的字典:

{"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']

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-03
    • 1970-01-01
    • 2014-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多