我有一个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']
但是,如何获取地址字段并将其保存到变量?
答案 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']