簡體   English   中英

如何從json列表數據中獲取特定索引

[英]How to get the specific index from a json list data

我們有我正在嘗試解析的 Json 數據,同時使用json & resquest通過url讀取數據。

示例傑森數據:

{"statusMessage": "OK", "statusCode": 200, "response": { "id": "15015", "name": "cDOT_stx7116_esx01_07-18-2021_18.00.00.0586", "startTime": 1626624000472, "mounted": false, "vmwareSnapshot": "Yes", "status": "Completed", "policy": "SCV_DLY_NoDR", "entities": [ { "entityName": "CISCAT-2016_CN-SKK", "quiesced": true, "uuid": "500334f5-fea6-2992-a543-e4bd56822913", "locations": [ "[std7116_esx01] CISCAT-2016_CN-SKK/CISCAT-2016_.vmtx" ] }, { "entityName": "ind4481", "quiesced": true, "uuid": "500ae9dd-60e7-2339-4c4c-557ba00a3696", "locations": [ "[stx7116_esx01] ind4481/ind4481.vmx" ] }}

我試過的:

response = requests.get('https://str001.example.com:8144/api/4.1/backups/'+y1, headers=headers, verify =False)
output = response.json()
data4 = output['response']['entities']

下面是我試圖用 For 循環做的事情,並試圖獲得所需的index但沒有奏效:

for k in data4:
    x1 = (k['locations'].rsplit("]", 3)[-3].split("[")[-1],k['entityName'],k['quiesced'])
    print (x1)

期望輸出

stx7116_esx01 CISCAT-2016_CN-SKK true

如果您提供的 json 對象是准確的,那么您可以嘗試:

locations = []
for i in range(0,len(data4),1):
    data_dict = data4[i]     # each element is a separate dictionary
    location = data_dict['locations'][0]    # as locations is a list with a sngle item you'll have to extract it this way
    location =  location[location.find('[') + 1 : location.finct(']')]    # slice the string on range of the indexes of '[' and ']'
    desired_output = location + ' ' + data_dict['entityName'] + ' ' + data_dict['quiesced']

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM