簡體   English   中英

您如何解析嵌套的 JSON 數據以獲取特定信息?

[英]How do you parsing nested JSON data for specific information?

我正在使用國家氣象服務 API,當您使用特定的 URL 時,您會得到 JSON 數據。 到目前為止,我的程序抓取了所有內容,包括 155 小時的天氣數據。

  • 簡而言之,我正在嘗試解析數據並獲取最近一小時的天氣,但所有內容都在嵌套數據結構中。

我的代碼,JSON 數據,以及更多信息如下。 任何幫助表示贊賞。

import requests
import json

def get_current_weather():  #This method returns json data from the api
    url = 'https://api.weather.gov/gridpoints/*office*/*any number,*any number*/forecast/hourly'
    response = requests.get(url)
    full_data = response.json()
    return full_data  

def main():   #Prints the information grabbed from the API
    print(get_current_weather())

if __name__ == "__main__":
    main()

在 JSON 響應中,在您獲得我想要獲得的“shortForecast”數據之前,我得到了 3 層。 第一個嵌套是'properties,之前的一切都與我的程序無關。 第二個嵌套是“周期”,每個周期都是一個新的小時,0 是最新的。 最后,我只需要在第一個或多個周期[0] 中獲取“shortForcast”。

{
    "@context": [
        "https://geojson.org/geojson-ld/geojson-context.jsonld",
        {
            "@version": "1.1",
            "wx": "https://api.weather.gov/ontology#",
            "geo": "http://www.opengis.net/ont/geosparql#",
            "unit": "http://codes.wmo.int/common/unit/",
            "@vocab": "https://api.weather.gov/ontology#"
        }
    ],
    "type": "Feature",
    "geometry": {
        "type": "Polygon",
        "coordinates": [
            [
                *data I'm not gonna add*
            ]
        ]
    },
    "properties": {
        "updated": "2021-02-11T05:57:24+00:00",
        "units": "us",
        "forecastGenerator": "HourlyForecastGenerator",
        "generatedAt": "2021-02-11T07:12:58+00:00",
        "updateTime": "2021-02-11T05:57:24+00:00",
        "validTimes": "2021-02-10T23:00:00+00:00/P7DT14H",
        "elevation": {
            "value": ,
            "unitCode": "unit:m"
        },
        "periods": [
            {
                "number": 1,
                "name": "",
                "startTime": "2021-02-11T02:00:00-05:00",
                "endTime": "2021-02-11T03:00:00-05:00",
                "isDaytime": false,
                "temperature": 18,
                "temperatureUnit": "F",
                "temperatureTrend": null,
                "windSpeed": "10 mph",
                "windDirection": "N",
                "icon": "https://api.weather.gov/icons/land/night/snow,40?size=small",
                "shortForecast": "Chance Light Snow",
                "detailedForecast": ""
            },
            {
                "number": 2,
                "name": "",
                "startTime": "2021-02-11T03:00:00-05:00",
                "endTime": "2021-02-11T04:00:00-05:00",
                "isDaytime": false,
                "temperature": 17,
                "temperatureUnit": "F",
                "temperatureTrend": null,
                "windSpeed": "12 mph",
                "windDirection": "N",
                "icon": "https://api.weather.gov/icons/land/night/snow,40?size=small",
                "shortForecast": "Chance Light Snow",
                "detailedForecast": ""
            },

好的,所以我不想再次編輯所有內容,所以這是新的 get_current_weather 方法。 我能夠進入'時期,但在那之后我仍然很難過。 這是新方法。

def get_current_weather():  
    url = 'https://api.weather.gov/gridpoints/ILN/82,83/forecast/hourly'
    response = requests.get(url)
    full_data = response.json()
    return full_data['properties'].get('periods')

對於字典 object,您可以通過多次使用索引來訪問嵌套元素。

因此,對於您的字典 object,您可以使用以下命令獲取主字典中鍵properties下鍵periods下字典列表中第一個元素的鍵shortForecast的值:

full_data['properties']['periods'][0]['shortForecast']

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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