如何处理 Json 响应以及如何在我的终端 window 中将值与此 Json 响应分开
How to handle Json response and How Can I sperate value from this Json response in my terminal window
`import requests
from requests.models import Response
api_key = "xxx"
city = "Houston"
Response = requests.get(
f"https://api.openweathermap.org/data/2.5/weather?q={city},us&appid={api_key}")
full_info = (Response.json()["weather"])
print(full_info)`
输出:
{'coord': {'lon': -95.3633, 'lat': 29.7633}, 'weather': [{'id': 800, 'main': 'Clear', 'description': 'clear sky', 'icon': '01n'}], 'base': 'stations', 'main': {'temp': 299.96, 'feels_like': 302.81, 'temp_min': 298.12, 'temp_max': 301.26, 'pressure': 1015, 'humidity': 84}, 'visibility': 10000, 'wind': {'speed': 0.89, 'deg': 231,
'gust': 4.02}, 'clouds': {'all': 1}, 'dt': 1627897632, 'sys': {'type': 2, 'id': 2006306, 'country': 'US', 'sunrise': 1627904498, 'sunset': 1627953218}, 'timezone': -18000, 'id': 4699066, 'name': 'Houston', 'cod': 200}
如果我用
full_info = response.json()["weather"]
输出:
[{'id': 800, 'main': 'Clear', 'description': 'clear sky', 'icon': '01n'}]
检查其类型后的列表
但我无法访问像 Main 或 description
这样的特定键值
如何实现不同变量中的所有值或只能访问我想要的值
如果我尝试使用索引打印
print(full_info[0])
输出保持不变
做full_info = Response.json()
然后
print(full_info['weather'][0]['id'])
`import requests
from requests.models import Response
api_key = "xxx"
city = "Houston"
Response = requests.get(
f"https://api.openweathermap.org/data/2.5/weather?q={city},us&appid={api_key}")
full_info = (Response.json()["weather"])
print(full_info)`
输出:
{'coord': {'lon': -95.3633, 'lat': 29.7633}, 'weather': [{'id': 800, 'main': 'Clear', 'description': 'clear sky', 'icon': '01n'}], 'base': 'stations', 'main': {'temp': 299.96, 'feels_like': 302.81, 'temp_min': 298.12, 'temp_max': 301.26, 'pressure': 1015, 'humidity': 84}, 'visibility': 10000, 'wind': {'speed': 0.89, 'deg': 231,
'gust': 4.02}, 'clouds': {'all': 1}, 'dt': 1627897632, 'sys': {'type': 2, 'id': 2006306, 'country': 'US', 'sunrise': 1627904498, 'sunset': 1627953218}, 'timezone': -18000, 'id': 4699066, 'name': 'Houston', 'cod': 200}
如果我用
full_info = response.json()["weather"]
输出:
[{'id': 800, 'main': 'Clear', 'description': 'clear sky', 'icon': '01n'}]
检查其类型后的列表
但我无法访问像 Main 或 description
如何实现不同变量中的所有值或只能访问我想要的值
如果我尝试使用索引打印
print(full_info[0])
输出保持不变
做full_info = Response.json()
然后
print(full_info['weather'][0]['id'])