簡體   English   中英

錯誤:無法解碼JSON對象

[英]Error: No JSON object could be decoded

這是我創建的智能鏡像中的天氣代碼。 我正在使用其他人的代碼,在運行該應用程序時遇到這兩個錯誤(該應用程序可以正常工作,但不會顯示天氣):

  1. ValueError:無法解碼JSON對象
  2. 錯誤:無法解碼JSON對象。 無法獲取天氣

     def get_weather(self): try: if latitude is None and longitude is None: # get location location_req_url = "http://freegeoip.net/json/%s" % self.get_ip() r = requests.get(location_req_url) location_obj = json.loads(r.text) lat = location_obj['latitude'] lon = location_obj['longitude'] location2 = "%s, %s" % (location_obj['city'], location_obj['region_code']) # get weather weather_req_url = "https://api.darksky.net/forecast/%s/%s,%s?lang=%s&units=%s" % (weather_api_token, lat,lon,weather_lang,weather_unit) else: location2 = "" # get weather weather_req_url = "https://api.darksky.net/forecast/%s/%s,%s?lang=%s&units=%s" % (weather_api_token, latitude, longitude, weather_lang, weather_unit) r = requests.get(weather_req_url) weather_obj = json.loads(r.text) degree_sign= u'\\N{DEGREE SIGN}' temperature2 = "%s%s" % (str(int(weather_obj['currently']['temperature'])), degree_sign) currently2 = weather_obj['currently']['summary'] forecast2 = weather_obj["hourly"]["summary"] icon_id = weather_obj['currently']['icon'] icon2 = None if icon_id in icon_lookup: icon2 = icon_lookup[icon_id] if icon2 is not None: if self.icon != icon2: self.icon = icon2 image = Image.open(icon2) image = image.resize((100, 100), Image.ANTIALIAS) image = image.convert('RGB') photo = ImageTk.PhotoImage(image) self.iconLbl.config(image=photo) self.iconLbl.image = photo else: # remove image self.iconLbl.config(image='') if self.currently != currently2: self.currently = currently2 self.currentlyLbl.config(text=currently2) if self.forecast != forecast2: self.forecast = forecast2 self.forecastLbl.config(text=forecast2) if self.temperature != temperature2: self.temperature = temperature2 self.temperatureLbl.config(text=temperature2) if self.location != location2: if location2 == ", ": self.location = "Cannot Pinpoint Location" self.locationLbl.config(text="Cannot Pinpoint Location") else: self.location = location2 self.locationLbl.config(text=location2) except Exception as e: traceback.print_exc() print( "Error: %s. Cannot get weather." % e) self.after(600000, self.get_weather) @staticmethod def convert_kelvin_to_fahrenheit(kelvin_temp): return 1.8 * (kelvin_temp - 273) + 32 

這是應用程序中天氣小部件的代碼。 我使用的模塊是:

from tkinter import *
import locale
import threading
import time
import requests
import json
import traceback
import feedparser

from PIL import Image, ImageTk
from contextlib import contextmanager

這些就是正在使用的模塊。

好的,經過很長時間的嘗試,我發現即使api令牌正在打印404(表示禁止使用),它也不會更改輸出,而錯誤的代碼部分是:

weather_req_url = "https://api.darksky.net/forecast/%s/%s,%s?lang=%s&units=%s" % (weather_api_token, latitude, longitude, weather_lang, weather_unit)

應該在哪里:

weather_req_url = "%s/%s,%s?lang=%s&units=%s" % (weather_api_token, latitude, longitude, weather_lang, weather_unit)

這是因為我的api密鑰應該是鏈接,因此“((https)://api.darksky.net/forecast/”也是

所以我從weather_req_url中取出了它

注意“()”中的https,因此不會形成可點擊的鏈接

暫無
暫無

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

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