我有下面的查詢存儲在我得到的變量中,我需要獲取'resource_status'的值。解析Python中的JSON
我需要 'UPDATE_IN_PROGRESS'
按照要求,把代碼在這裏。變量evntsdata正在存儲事件列表。
try:
evntsdata = str(hc.events.list(stack_name)[0]).split(" ") # this is the variable that is getting the JSON response (or so)
#print(evntsdata[715:733])
#event_handle = evntsdata[715:733]
if event_handle == 'UPDATE_IN_PROGRESS':
loopcontinue = True
while loopcontinue:
evntsdata = str(hc.events.list(stack_name)[0]).split(" ")
#event_handle = evntsdata[715:733]
if (event_handle == 'UPDATE_COMPLETE'):
loopcontinue = False
print(str(timestamp()) + " " + "Stack Update is Completed!" + ' - ' + evntsdata[-3] + ' = ' + evntsdata[-1])
else:
print(str(timestamp()) + " " + "Stack Update in Progress!" + ' - ' + evntsdata[-3] + ' = ' + evntsdata[-1])
time.sleep(10)
else:
print("No updates to perform")
exit(0)
except AttributeError as e:
print(str(timestamp()) + " " + "ERROR: Stack Update Failure")
raise
打印(evntsdata)具有以下結果
['<Event', "{'resource_name':", "'Stackstack1',", "'event_time':", "'2017-05-26T12:10:43',", "'links':", "[{'href':", "'x',", "'rel':", "'self'},", "{'href':", "'x',", "'rel':", "'resource'},", "{'href':", "'x',", "'rel':", "'stack'}],", "'logical_resource_id':", "'Stackstack1',", "'resource_status':", "'UPDATE_IN_PROGRESS',", "'resource_status_reason':", "'Stack", 'UPDATE', "started',", "'physical_resource_id':", "'xxx',", "'id':", "'xxx'}>"]
這不是JSON。看起來你已經打印了一些Event對象。請*用[編輯]顯示您的代碼,並且還包括解析此 –
時的嘗試,請檢查編輯。讓我知道如果你需要任何其他信息 –
無論'hc.events.list(stack_name)[0]'你不應該'str()'和'.split()'它。這在很多層面上都是錯誤的。我假設這個'Event'類有一些方法來檢索你正在尋找的字段。使用它們。 – freakish