我dict
類型的響應語法:如何訪問字典值?
{
'StoppingInstances': [
{
'InstanceId': 'string',
'CurrentState': {
'Code': 123,
'Name': 'pending'|'running'|'shutting-down'|'terminated'|'stopping'|'stopped'
},
'PreviousState': {
'Code': 123,
'Name': 'pending'|'running'|'shutting-down'|'terminated'|'stopping'|'stopped'
}
},
]
}
現在,如果我需要檢查,如果在currentState是stopping
怎麼辦呢?
print "Stopping instance Now",response['StoppingInstances']
for instance in response['StoppingInstances']:
if instance['CurrentState']['Name'] == "stopping":
print "Still Stooping"
if instance['CurrentState']['Name'] == "stopped":
print "Instance Stopped"
print "Now Starting the instance"
response_new = client.start_instances(InstanceIds=[instance_id])
for start_instance in response_new['StartingInstances']:
if start_instance['CurrentState']['Name'] == "running":
print "Instance is UP and running"
else:
print "Some Error occured!!"
只要看看你的數據。 'response ['StoppingInstances']'是一個列表(數組,在JSON中),你需要用一個整數索引它。 – jonrsharpe