我正在使用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":{"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}
代码:
response = resp.json()
if response['cod'] != '404':
main_json = response['main']
temperature = main_json['temp']
答案 0 :(得分:0)
您明确是直接意思吗?
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']