Python 有没有办法显式调用json参数?
我正在使用OpenWeatherMap编写程序,我有关于天气的JSON文件: 有没有办法从Python 有没有办法显式调用json参数?,python,json,Python,Json,我正在使用OpenWeatherMap编写程序,我有关于天气的JSON文件: 有没有办法从“main”:{“temp”:…显式保存信息,还是只有我这样做 {"coord": {"lon":145.77,"lat":-16.92}, "weather":[{"id":803,"main":"Clouds","description":"broken clouds","icon":"04n"}], "base":"cmc stations", "main":{"tem
“main”:{“temp”:…
显式保存信息,还是只有我这样做
{"coord":
{"lon":145.77,"lat":-16.92},
"weather":[{"id":803,"main":"Clouds","description":"broken clouds","icon":"04n"}],
"base":"cmc stations",
"main":{"temp":293.25,"pressure":1019,"humidity":83,"temp_min":289.82,"temp_max":295.37},
"wind":{"speed":5.1,"deg":150},
"clouds":{"all":75},
"rain":{"3h":3},
"dt":1435658272,
"sys":{"type":1,"id":8166,"message":0.0166,"country":"AU","sunrise":1435610796,"sunset":1435650870},
"id":2172797,
"name":"Cairns",
"cod":200}
代码:
你的意思是直接的吗
temperature = response['main']['temp']
编辑:
将json保存到名为data.json的文件后,以下代码在我的机器上运行:
import json
with open('data.json') as json_file:
response = json.load(json_file)
if response['cod'] != '404':
main_json = response['main']['temp']
是的,我试过了,但它不起作用温度=response['main']['temp']KeyError:'main'那么
main\u json=response['main']
在你的问题中如何起作用呢?我不知道why@PawełDługoszmain_json=response['main']
不可能工作,但替换为main_json=response['main']时失败['temp']
原因是KeyError:'main'
。是否可能是您更改了导致此问题的其他内容?是的,您是对的,它不起作用,我对jsonDon有问题,您不需要从response=resp.json()['coord'开始]
?如果这是来自请求
,您应该能够在中执行分别为_status()提高
,而不是手动检查状态代码。请提供您的其余代码
import json
with open('data.json') as json_file:
response = json.load(json_file)
if response['cod'] != '404':
main_json = response['main']['temp']