使用python-list索引解析JSON必须是整数而不是str

使用python-list索引解析JSON必须是整数而不是str,python,json,Python,Json,我是python初学者,正在尝试解析以下JSON。我无法找到如何获得歌曲的艺术家名称和标题 { "status": { "msg": "Success", "code": 0, "version": "1.0" }, "metadata": { "music": [ { "external_ids":

我是python初学者,正在尝试解析以下JSON。我无法找到如何获得歌曲的艺术家名称和标题

{
       "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": 464321089
                           }
                       },
                       "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

请帮助查看以下数据:

                       "artists": [
                           {
                               "id": "6vWDO969PvNqNYHIOW5v0m"
                           }
                       ],
                       "track": {
                           "id": "3qSMg1lhn4jDwWlI9xCVyK"
                       }
您的“艺术家”数据是一个列表,这就是为什么您不能像
[“艺术家”][“id”]
那样访问它,但
[“艺术家”][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'
[1]中的
:a=[{“你好”:“世界”},“是的,我是”]
在[2]中:a[“你好”]
---------------------------------------------------------------------------
TypeError回溯(最近一次调用上次)
在()
---->1A[“你好”]
TypeError:列表索引必须是整数,而不是str
在[3]中:a[0]
Out[3]:{'hello':'world'}
在[4]:a[0][“你好”]
出[4]:“世界”
In[5]:a[1]
出[5]:“是的,我是”
由于“a”是一个列表,访问其元素可以通过索引完成,即a[0]、a[1]


a[0]
是一个字典
{“hello”:“world”}
,因此可以通过首先访问其索引0,然后访问键“hello”来访问字典值,就像
a[0][“hello”

一样,这是一种更简单的方法:

import json
from pprint import pprint

with open('D:/data.json') as data_file:
    data = json.load(data_file)
pprint(data)
尝试上面的代码,这将以字典的形式打印您的文件。 然后,您可以通过使用它的键索引来简单地访问它的元素 访问它的值,例如:

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

只需确保您试图访问的元素是列表还是字典,如列表
[]
中所述,您可能需要使用索引,如第二个示例所述。

您是否看到JSON数据中的方括号?这些表示
列表
s,其索引必须为整数。