【问题标题】:Filtering the JSON Elements in Python not working在 Python 中过滤 JSON 元素不起作用
【发布时间】:2021-09-04 08:26:52
【问题描述】:

我在 Python 中使用 Yahoo Weather API 制作了一个简单的天气应用程序。一切都很好,但问题是我可以从 JSON 过滤城市和国家/地区,但我还想获得温度,这导致我错误。谢谢!

代码:

from tkinter import *
from tkinter import messagebox

import requests
import json

root = Tk()
bg1 = PhotoImage(file="weather.png")

root.title("Weather App")
root.geometry('1200x675')

lbl2 = Label(root, image=bg1)
lbl2.place(x=0, y=0, relwidth=1, relheight=1)

lbl = Label(root, text="Welcome to the Weather App!")
lbl['font'] = "Montserrat 20"
lbl['bg'] = "#293b6b"
lbl['fg'] = "white"
lbl.place(x=400, y=10)

lbl_lat= Label(root, text="Enter latitude:")
lbl_lat['font'] = "Montserrat 8"
lbl_lat['bg'] = "#293b6b"
lbl_lat['fg'] = "white"
lbl_lat.place(x=300, y=620)


ent1_lot = Entry(root)
ent1_lot['font'] = "Montserrat 8"
ent1_lot['bg'] = "#293b6b"
ent1_lot['fg'] = "white"
ent1_lot.place(x=400, y=620)


lbl = Label(root, text="Enter long:")
lbl['font'] = "Montserrat 8"
lbl['bg'] = "#293b6b"
lbl['fg'] = "white"
lbl.place(x=300, y=640)


ent2_lot = Entry(root)
ent2_lot['font'] = "Montserrat 8"
ent2_lot['bg'] = "#293b6b"
ent2_lot['fg'] = "white"
ent2_lot.place(x=400, y=640)

def clicked_2():
    url = "https://yahoo-weather5.p.rapidapi.com/weather"

    querystring = {"lat": ent1_lot.get(), "long":ent2_lot.get(), "format": "json", "u": "c"}

    headers = {
        'x-rapidapi-host': "yahoo-weather5.p.rapidapi.com",
        'x-rapidapi-key': ""
    }

    response = requests.request("GET", url, headers=headers, params=querystring)

    json_data = json.loads(response.text)
    location = json_data['location']
    city = location['city']
    country = location['country']

    lbl_ci = Label(root, text=city)
    lbl_ci['font'] = "Montserrat 20"
    lbl_ci['fg'] = "gold"
    lbl_ci.place(x=400, y=340)

    lbl_c = Label(root, text=country)
    lbl_c['font'] = "Montserrat 20"
    lbl_c['fg'] = "gold"
    lbl_c.place(x=400, y=300)


btn = Button(root, text="Get weather informations", fg="white", bg="black",command= clicked_2)
btn['font'] = "Montserrat 10"
btn.place(x=1000, y=620)

def clicked():
    messagebox.showinfo('Weather App Terms and Conditions', "Terms and Conditions\nLast updated: September 04, 2021\nPlease read these terms and conditions carefully before using Our Service."
        )



btn2 = Button(text="Terms and Conditions", fg="white", bg="black", command= clicked)
btn2['font'] = "Montserrat 10"
btn2.place(x=15, y=620)








root.mainloop()

这是来自 JSON 的响应:

{
  "location": {
    "city": "Al Majaridah",
    "region": " Aseer",
    "woeid": 1937813,
    "country": "Saudi Arabia",
    "lat": 19.1667,
    "long": 41.9715,
    "timezone_id": "Asia/Riyadh"
  },
  "current_observation": {
    "wind": {
      "chill": 37,
      "direction": 248,
      "speed": 6.4
    },
    "atmosphere": {
      "humidity": 54,
      "visibility": 16.01,
      "pressure": 937,
      "rising": 0
    },
    "astronomy": {
      "sunrise": "5:58 am",
      "sunset": "6:24 pm"
    },
    "condition": {
      "code": 34,
      "text": "Mostly Sunny",
      "temperature": 33
    },
    "pubDate": 1630754100
  },
  "forecasts": [
    {
      "day": "Sat",
      "date": 1630724400,
      "low": 28,
      "high": 36,
      "text": "Thunderstorms",
      "code": 4
    },
    {
      "day": "Sun",
      "date": 1630810800,
      "low": 28,
      "high": 38,
      "text": "Thunderstorms",
      "code": 4
    },
    {
      "day": "Mon",
      "date": 1630897200,
      "low": 29,
      "high": 37,
      "text": "Thunderstorms",
      "code": 4
    },
    {
      "day": "Tue",
      "date": 1630983600,
      "low": 27,
      "high": 38,
      "text": "Sunny",
      "code": 32
    },
    {
      "day": "Wed",
      "date": 1631070000,
      "low": 27,
      "high": 39,
      "text": "Mostly Sunny",
      "code": 34
    },
    {
      "day": "Thu",
      "date": 1631156400,
      "low": 27,
      "high": 40,
      "text": "Partly Cloudy",
      "code": 30
    },
    {
      "day": "Fri",
      "date": 1631242800,
      "low": 27,
      "high": 39,
      "text": "Partly Cloudy",
      "code": 30
    },
    {
      "day": "Sat",
      "date": 1631329200,
      "low": 27,
      "high": 39,
      "text": "Mostly Sunny",
      "code": 34
    },
    {
      "day": "Sun",
      "date": 1631415600,
      "low": 27,
      "high": 38,
      "text": "Thunderstorms",
      "code": 4
    },
    {
      "day": "Mon",
      "date": 1631502000,
      "low": 27,
      "high": 39,
      "text": "Mostly Sunny",
      "code": 34
    },
    {
      "day": "Tue",
      "date": 1631588400,
      "low": 25,
      "high": 36,
      "text": "Thunderstorms",
      "code": 4
    }
  ]
}

【问题讨论】:

  • 你在哪里遇到错误?
  • 错误:Tkinter 回调 Traceback 中的异常(最近一次调用最后一次):文件“C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.2800.0_x64__qbz5n2kfra8p0\lib\tkinter_init_ .py”,第 1892 行,在 call 中返回 self.func(*args) 文件“C:/Users/user/PycharmProjects/GUIProjekt/main.py”,第 65 行,在clicked_2 condition=json_data['condition'] KeyError: 'condition'
  • 代码:json_data = json.loads(response.text) location = json_data['location'] city = location['city'] country = location['country'] condition=json_data['condition'] temperature=condition['temprature'] lbl_t = Label(root, text=temperature) lbl_t['font'] = "Montserrat 20" lbl_t['fg'] = "gold" lbl_t.place(x=400, y=330)
  • 您的 json 响应不包含“条件”键

标签: python json api


【解决方案1】:

这应该让你得到温度:

json_data['current_observation']['condition']['temperature']

【讨论】:

    【解决方案2】:

    要获得温度,您只需要做:

    temperature = json_data['current_observation']['condition']['temperature]
    

    我认为你错过了 current_observation 部分。要查看它,我将 json 放入 jq,但您可以使用任何其他 json 修饰符

    【讨论】:

      【解决方案3】:

      试试看

      json_data = json.loads(response.text)     
      location = json_data['location']     
      city = location['city']     
      country = location['country']     
      condition=json_data['current_observation']['condition']   
      temperature=condition['temperature']      
      

      【讨论】:

        猜你喜欢
        • 2020-05-02
        • 1970-01-01
        • 1970-01-01
        • 2015-04-02
        • 2023-03-17
        • 2021-04-02
        • 1970-01-01
        • 1970-01-01
        • 2021-09-09
        相关资源
        最近更新 更多