我正在使用
flask
在使用服务api的web应用程序中生成JSON响应。函数的以下部分工作正常,并返回JSON文本输出:
def get_weather(query = 'london'):
api_url = "http://api.openweathermap.org/data/2.5/weather?q={}&units=metric&appid=XXXXX****2a6eaf86760c"
query = urllib.request.quote(query)
url = api_url.format(query)
response = urllib.request.urlopen(url)
data = response.read()
return data
返回的输出是:
{"coord":{"lon":-0.13,"lat":51.51},"weather":[{"id":803,"main":"Clouds","description":"broken clouds","icon":"04d"}],"base":"cmc stations","main":{"temp":12.95,"pressure":1030,"humidity":68,"temp_min":12.95,"temp_max":12.95,"sea_level":1039.93,"grnd_level":1030},"wind":{"speed":5.11,"deg":279.006},"clouds":{"all":76},"dt":1462290955,"sys":{"message":0.0048,"country":"GB","sunrise":1462249610,"sunset":1462303729},"id":2643743,"name":"London","cod":200}
这意味着
data
是一根绳子,不是吗?
然而
,评论
return data
然后添加以下两行:
jsonData = json.loads(data)
return jsonData
生成以下错误:
TypeError:JSON对象必须是str,而不是“bytes”
发生了什么?
数据
,
JSON对象
,以前作为字符串返回!我需要知道错误在哪里?