簡體   English   中英

使用 Python 讀取深度嵌套的 Json 對象

[英]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.

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