我正在访问一个API,这里是示例(https://developer.pagerduty.com/documentation/rest/escalation_policies/on_call)
SUBDOMAIN='XXX'
API_ACCESS_KEY='XXX'
headers = {
'Authorization': 'Token token={0}'.format(API_ACCESS_KEY),
'Content-type': 'application/json',
}
url = 'https://{0}.pagerduty.com/api/v1/escalation_policies/on_call'.format(SUBDOMAIN)
r = requests.get(url,headers=headers)
objData = r.json()
for objPolicy in objData['escalation_policies']:
print objPolicy['name']
for objOnCall in objPolicy['on_call']:
print objOnCall['level']
print objOnCall['start']
print objOnCall['end']
for objUser in objOnCall['user']:
print objUser['name']
我目前得到的错误
print objUser['name']
TypeError: string indices must be integers
如果我的理解是正确的,[]是一个列表,{}是一个对象?所以我试图访问一个对象作为一个列表,这就是为什么它不工作
对于新手来说,筑巢的数量是很难理解的。有人能解释一下并告诉我如何访问该通话策略中的每个用户吗
谢谢
根据API documentation,在
user
下有一个对象(想想“dictionary”):要反映这一点,请将代码修改为:
相关问题 更多 >
编程相关推荐