2017-02-18 1 views
2

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?

+4

forecast = item [ '날씨'] [0] [ '메인'] –

답변

0

weather 1 개 요소의 목록 str에 있지이어야한다. 따라서 다음과 같은 방법으로 데이터에 액세스해야합니다.
forecast = item['weather'][0]['main']

관련 문제