![](/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.