2016-04-27 203 views
1

我試圖在Python中提取各個路徑ID。獲取JSON密鑰錯誤

當我打印route_iddata['route']我收到KeyError:'route'。這是什麼意思?我看到有一個數組'mode'和另一個數組'route'

這是JSON數據:

{ "mode":[ 
     { 
     "route_type":"0", 
     "route":[ 
      { 
       "route_id":"Green-B", 
       "route_name":"Green Line B" 
      }, 
      { 
       "route_id":"Green-C", 
       "route_name":"Green Line C" 
      }, 
      { 
       "route_id":"Green-D", 
       "route_name":"Green Line D" 
      }, 
      { 
       "route_id":"Green-E", 
       "route_name":"Green Line E" 
      }, 
      { 
       "route_id":"Mattapan", 
       "route_name":"Mattapan Trolley" 
      } 
     ], 
     "mode_name":"Subway" 
     }, 
+1

'data [「mode」] [0] [「route」] [0] [「route_id」]' – ppaulojr

+1

謝謝你們倆! –

+0

陌生人:請閱讀[_當某人回答我的問題時該怎麼辦?_(http://stackoverflow.com/help/someone-answers) – martineau

回答

3

您可以通過data["mode"][0]["route"][0]["route_id"]訪問route_id