2015-09-26 24 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'] 

只要确保其元素,您试图访问,羯羊它是一个列表或字典,如列表[]的情况下,您可能需要使用索引,如第二个示例中所述。

相关问题