[英]Python Dictionary JSON object
我正在發出獲取請求,如下所示:
testHeaders = {'X-Api-Key':'myAPIKey'}
testURL = 'https://api.mytest.com/v2/test.json'
r = requests.get(testURL, headers=testHeaders)
status = r.status_code
response = r.json()
我的響應返回一個包含兩個鍵的字典對象,然后每個鍵中包含多個值。
如何分配每個密鑰,然后訪問和評估其中的數據?
我在網上看過,似乎找不到任何相關的內容。
JSON結構示例
[{"id":000001,"account":000001,"name":"testName1","host":testHost1","test_color":"green"}
我有如上所述的多條記錄
如果知道您有一個test1
鍵,則可以搜索green
(假設您的json有兩個帶有嵌套值的鍵(test1和test2)):
for key, value in response.items():
# key will return key test1 and test2
# value will return a tuple 'testValue':'green'
if key == 'test1':
if 'test_color' in value:
color = value[test_color]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.