![](/img/trans.png)
[英]Is there a better way to access deeply nested JSON-like objects in Python?
[英]Reading deeply nested Json objects with Python
我想讀取深度嵌套的 Json 對象的值。 我對達到temperature
值很感興趣。
{
"weatherChannel": [
{
"week": {
"location": [
{
"info": [
{
"temperature": 5
}
]
}
]
}
}
]
}
我能夠通過執行以下操作來weatherChannel['week']['location']
location
信息: weatherChannel['week']['location']
但我無法進一步向下。
任何關於如何實現這一點的建議將不勝感激。
d = {
"weatherChannel": [
{
"week": {
"location": [
{
"info": [
{
"temperature": 5
}
]
}
]
}
}
]
}
d["weatherChannel"][0]["week"]["location"][0]["info"][0]["temperature"]
這提供了您所需要的。 您需要了解[]
表示 List 並且您可以通過它們的位置訪問 List 元素(第一個位置是 0,而不是 1)。 雖然{}
表示字典包含雙key:value
例如像"temperature":5
和你通過他們的密鑰訪問該值。
weatherChannel 包含一個列表,因此將是weatherChannel[0]["week"]["location"]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.