[英]Python - accessing a value within in a nested dictionary within a list
我有一個類似於以下內容的JSON API響應:
json_data=
{
"sales_list": [
{
"date": "all",
"country": "all",
"units": {
"product": {
"promotions": 0,
"downloads": 1,
"updates": 2,
"refunds": 3
},
"iap": {
"promotions": 0,
"sales": 0,
"refunds": 0
}
},
"revenue": {
"product": {
"promotions": "0.00",
"downloads": "0.00",
"updates": "0.00",
"refunds": "0.00"
},
"iap": {
"promotions": "0.00",
"sales": "0.00",
"refunds": "0.00"
},
"ad": "0.00"
}
}
],
"next_page": null,
"code": 200,
"prev_page": null,
"vertical": "apps",
"page_num": 1,
"iap_sales_list": [],
"currency": "USD",
"page_index": 0,
"market": "ios"
}
我正在使用Python,並嘗試訪問響應中的第一個“下載”值。 因此,我需要從sales_list(字典中的列表)>單位(字典)>產品(字典)>下載開始。 我如何深入挖掘這些多層以訪問此單個值?
我已經看到了有關在列表中的字典或嵌套字典中訪問值的問題。 但是我對如何在字典中的列表和列表中的字典之間/之間導航感到有些困惑。 任何幫助將不勝感激。
相似的問題: Python訪問嵌套的JSON數據
那是你需要的嗎?
print(json_data['sales_list'][0]['units']['product']['downloads'])
它給出輸出1
回答您的問題:
如您所見,您的json字段sales_list
是一元字典列表
[ {dictionary with field you need}, {other dict}, .. ]
因此,您需要指定要訪問的列表元素的索引-如果是一個元素列表,它將為[0]
因為列表的第一個元素包含您需要的字段
>>> dict['sales_list'][0]['units']['products']['downloads']
1
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.