如何访问 Class 字典类型的数据?

How to access data within Class Dict type?

我正在尝试使用 json 格式的特定天气数据,并使用 Python3 脚本访问数据的某些部分。实际数据尚未在线提供,因此我使用 json 格式提供的示例。这是 json 文件内容:

    "observations": [{
        "stationID": "KNCCARY89",
        "obsTimeUtc": "2019-02-04T14:53:14Z",
        "obsTimeLocal": "2019-02-04 09:53:14",
        "neighborhood": "Highcroft Village",
        "softwareType": "GoWunder 1337.9041ac1",
        "country": "US",
        "solarRadiation": 436.0,
        "lon": -78.8759613,
        "realtimeFrequency": null,
        "epoch": 1549291994,
        "lat": 35.80221176,
        "uv": 1.2,
        "winddir": 329,
        "humidity": 71,
        "qcStatus": 1,
        "imperial": {
            "temp": 53,
            "heatIndex": 53,
            "dewpt": 44,
            "windChill": 53,
            "windSpeed": 2,
            "windGust": null,
            "pressure": 30.09,
            "precipRate": 0.0,
            "precipTotal": 0.0,
            "elev": 413
        }
    }]
}

这是我用来从我的 Raspberry 上的文件访问此示例 json 数据的简单 python 脚本:

import json
from pprint import pprint

with open('data.json') as f:
    weather = json.load(f)

pprint(weather)

数据打印得很好,但我一直在努力使用嵌入数据!

当我查询类型 "type(weather)" 时,答案是“

似乎唯一有效的查询是“pprint(weather['observations']),它显示了 'observations' 下面的所有 json 数据,但我不知道如何低于这个!

我是否必须将数据转换为另一个 'type'?

weather['observations'] 上面的JSON 好像是一个只有一个元素的数组。在 Python 中,天气 ['observations'] 应该是一个列表,要访问它的第一个元素,您可以编写

weather['observations'][0]

由此,您应该能够访问子元素,例如

weather['observations'][0]['stationID']