2015-09-26 62 views
-2

我是python的初學者,並且試圖解析下面的JSON。我無法找到如何獲取歌曲的藝術家姓名和標題。用python-list索引解析JSON必須是整數而不是str

{ 
     "status": { 
      "msg": "Success", 
      "code": 0, 
      "version": "1.0" 
     }, 
     "metadata": { 
      "music": [ 
       { 
        "external_ids": { 
         "isrc": "USSM10603618", 
         "upc": "888880170897" 
        }, 
        "play_offset_ms": 8920, 
        "external_metadata": { 
         "spotify": { 
          "album": { 
           "id": "0JLv6iVbeiy4Dh2eIw6FKI" 
          }, 
          "artists": [ 
           { 
            "id": "6vWDO969PvNqNYHIOW5v0m" 
           } 
          ], 
          "track": { 
           "id": "3qSMg1lhn4jDwWlI9xCVyK" 
          } 
         }, 
         "itunes": { 
          "album": { 
           "id": 464320979 
          }, 
          "artists": [ 
           { 
            "id": 1419227 
           } 
          ], 
          "track": { 
           "id":89 
          } 
         }, 
         "deezer": { 
          "album": { 
           "id": 72429 
          }, 
          "artists": [ 
           { 
            "id": 145 
           } 
          ], 
          "genres": [ 
           { 
            "id": 132 
           } 
          ], 
          "track": { 
           "id": 551232 
          } 
         } 
        }, 
        "title": "Listen (From the Motion Picture \"Dreamgirls\")", 
        "duration_ms": "217786", 
        "album": { 
         "name": "B'Day Deluxe Edition" 
        }, 
        "acrid": "4660601066a3153acf15eabe2868572b", 
        "genres": [ 
         { 
          "name": "Pop" 
         } 
        ], 
        "artists": [ 
         { 
          "name": "Beyoncé" 
         } 
        ] 
       } 
      ], 
      "timestamp_utc": "2015-07-27 10:35:28" 
     }, 
     "result_type": 0 
} 

我的代碼是:

json_r=json.loads(res) 
     print(json_r) 
     for i in json_r: 
      song_name=json_r.metadata['music']['title'] 
      print song_name 
      artist=json_r['metadata']['music']['artists']['name'] 
      s_t_id=json_r['metadata']['music']['external_metadata']['spotify']['track']['id'] 
      s_a_id=json_r['metadata']['music']['external_metadata']['spotify']['artists']['id'] 

我收到以下錯誤: 列表索引必須是整數str的

請幫

+2

您是否看到JSON數據中的方括號?那些表示「列表」,其索引必須是整數。 – TigerhawkT3

回答

0

看到這個數據:

     "artists": [ 
          { 
           "id": "6vWDO969PvNqNYHIOW5v0m" 
          } 
         ], 
         "track": { 
          "id": "3qSMg1lhn4jDwWlI9xCVyK" 
         } 

你的「artists」數據是一個列表,這就是爲什麼你不能像["artists"]["id"]那樣訪問它,但是["artists"][0]["id"]會工作。

參見下面的例子用於更清楚地瞭解:

In [1]: a = [{"hello": "world"}, "yes I am"] 

In [2]: a["hello"] 
--------------------------------------------------------------------------- 
TypeError         Traceback (most recent call last) 
<ipython-input-2-817deb35df57> in <module>() 
----> 1 a["hello"] 

TypeError: list indices must be integers, not str 

In [3]: a[0] 
Out[3]: {'hello': 'world'} 

In [4]: a[0]["hello"] 
Out[4]: 'world' 

In [5]: a[1] 
Out[5]: 'yes I am' 

由於「a」被一列表,訪問其元件可以由指數,即完成。一個[0],A [1] ...

a[0]是一個字典{"hello": "world"},所以字典值可以通過第一訪問其索引爲0,那麼關鍵 「你好」,像a[0]["hello"]進行訪問。

1

他們是這樣做的更簡單的方法:

import json 
from pprint import pprint 

with open('D:/data.json') as data_file: 
    data = json.load(data_file) 
pprint(data) 

試試上面的代碼,這將打印文件在字典中的條款。 然後你可以simpley訪問它的元素通過使用它的關鍵指標,以 訪問它的值是這樣的:

print data['status']['msg'] 
print data['metadata']['music'][0]['album']['name'] 

只要確保其元素,您試圖訪問,羯羊它是一個列表或字典,如列表[]的情況下,您可能需要使用索引,如第二個示例中所述。

相關問題