This is the JSON output from the API
{
"list":[
{
"dt": 1490791600,
"temp": {
"day": 58.26,
"min": 39.65,
"max": 67.37
}
"weather": [
{
"id": 500,
"main": "Rain",
"description": "light rain",
"icon": "10d"
}
]
{
"dt": 1480878000,
"temp": {
"day": 57.34,
"min": 40.11,
"max": 68.23
}
"weather": [
{
"id": 501,
"main": "Showers",
"description": "possible rain",
"icon": "11d"
}
]
]
}
This is my code. I want to try and pull the "id" for all of the weather dates.
json_obj = urllib2.urlopen(url)
data = json.load(json_obj)
for item in data['list']:
temperature = item['temp']['min']
forecast = item['weather']['main']
I am able to pull the temperature data without an issue but I get a TypeError: list indices must be integers not str when I try and pull the forecast data. I'm thinking this is due to list vs dictionary concepts. How can I pull the "id" values from this JSON data?
weather
是1个元素的列表。因此,您应该按以下方式访问数据。forecast = item['weather'][0]['main']
相关问题 更多 >
编程相关推荐