从字典中的列表中获取字典值

时间:2019-02-19 07:56:03

标签: python-3.x

我希望获取天气内部的description字段的值。

{'coord': {'lon': 73.85, 'lat': 18.52}, 'weather': [{'id': 800, 'main': 'Clear', 'description': 'clear sky', 'icon': '01d'}], 'base': 'stations', 'main': {'temp': 305.381, 'pressure': 949.7, 'humidity': 31, 'temp_min': 305.381, 'temp_max': 305.381, 'sea_level': 1023.73, 'grnd_level': 949.7}

我看过很多帖子,我正在尝试以下操作:

r1 = requests.get('http://api.openweathermap.org/data/2.5/weather?q=Pune,in&APPID=5ad6ec2537bfb0d574363e115c2d0041')
print(r1.status_code)
json_data = json.loads(r1.text)
print(json_data)
print("Weather is" ,json_data["weather"][0])

但是以上内容使我获取了字典中的所有值。 实现此目标的最佳方法是什么? 谢谢。

1 个答案:

答案 0 :(得分:1)

使用 JSONObject jsonObj = new JSONObject(response); String response_value = jsonObj.getString("response"); 访问描述字段。

相关问题