简体   繁体   English

使用 Python 读取深度嵌套的 Json 对象

[英]Reading deeply nested Json objects with Python

I want to read the value of a deeply nested Json object.我想读取深度嵌套的 Json 对象的值。 I am interested in reaching the temperature value.我对达到temperature值很感兴趣。

{
    "weatherChannel": [
       {
           "week": {  
                "location": [
                    {
                        "info": [ 
                            {
                                "temperature": 5
                            }
                         ]
                     }
                 ]
            } 
        }
     ]
 }

I am capable of reaching the location information by doing: weatherChannel['week']['location'] but I am unable to go further down.我能够通过执行以下操作来weatherChannel['week']['location'] location信息: weatherChannel['week']['location']但我无法进一步向下。

Any suggestions on how this can be achieved would be appreciated.任何关于如何实现这一点的建议将不胜感激。

d = {
     "weatherChannel": [
        {
            "week": {  
                 "location": [
                     {
                         "info": [ 
                             {
                                 "temperature": 5
                             }
                          ]
                      }
                  ]
             } 
         }
      ]
  }

d["weatherChannel"][0]["week"]["location"][0]["info"][0]["temperature"]

That provides what you need.这提供了您所需要的。 You need to understand that [] denotes List and you access to List elements through their position (first position is 0, not 1).您需要了解[]表示 List 并且您可以通过它们的位置访问 List 元素(第一个位置是 0,而不是 1)。 While {} denotes dictionary which contains pairs key:value like for example "temperature":5 and you access the values through their keys.虽然{}表示字典包含双key:value例如像"temperature":5和你通过他们的密钥访问该值。

weatherChannel 包含一个列表,因此将是weatherChannel[0]["week"]["location"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM