【发布时间】:2020-09-09 14:34:54
【问题描述】:
我正在尝试从返回的 REST API 输出中访问某些值。我已转换为字典,现在我正在尝试从“列表”开始访问列表中的元素:[
rest_response = {
"total": 2,
"offset": 0,
"limit": 25,
"list": [
{"id": 2233,
"url": "/v1/test/v2",
"enabled": true,
"info": {
"reason": "N/A",
"policy_name": "test",
"statuschk_tm": "2020-09-07 07:00:01",
"lock": "1",
"TYPE": "1"
}
]
}
之前我已经能够编写一个 for 循环 a 来搜索列表中的值,如下所示。
rest_response = rest_response['list']
for info in rest_response:
id = info['id']
print(id)
但是,如果我使用类似的 for 循环来搜索在 "info": { 之后开始的值,则会出现关键错误。
rest_response = rest_response['info']
for info in rest_response:
name = info['policy_name']
print(name)
我查看过相关帖子,但是当我尝试访问时,我得到“TypeError:列表索引必须是整数或切片,而不是 str”
Python Accessing Nested JSON Data Having trouble with nested Python3 dictionary
关于我可以做些什么来查看“信息”中的值的任何想法:向前?
【问题讨论】:
标签: python-3.x