Python 尝试从JSON对象生成列表(TypeError列表索引必须是整数或片,而不是str)
我已从API检索到一个JSON对象。JSON对象如下所示:Python 尝试从JSON对象生成列表(TypeError列表索引必须是整数或片,而不是str),python,json,Python,Json,我已从API检索到一个JSON对象。JSON对象如下所示: {'copyright': 'Copyright (c) 2020 The New York Times Company. All Rights ' 'Reserved.', 'response': {'docs': [{'_id': 'nyt://article/e3e5e5e5-1b32-5e2b-aea7-cf20c558dbd3', 'abstract
{'copyright': 'Copyright (c) 2020 The New York Times Company. All Rights '
'Reserved.',
'response': {'docs': [{'_id': 'nyt://article/e3e5e5e5-1b32-5e2b-aea7-cf20c558dbd3',
'abstract': 'LEAD: RESEARCHERS at the Brookhaven '
'National Laboratory are employing a novel '
'model to study skin cancer in humans: '
'they are exposing tiny tropical fish to '
'ultraviolet radiation.',
'byline': {'organization': None,
'original': 'By Eric Schmitt',
'person': [{'firstname': 'Eric',
'lastname': 'Schmitt',
'middlename': None,
'organization': '',
'qualifier': None,
'rank': 1,
'role': 'reported',
'title': None}]},
'document_type': 'article',
'headline': {'content_kicker': None,
'kicker': None,
'main': 'Tiny Fish Help Solve Cancer '
'Riddle',
'name': None,
'print_headline': 'Tiny Fish Help Solve '
'Cancer Riddle',
'seo': None,
'sub': None},
'keywords': [{'major': 'N',
'name': 'organizations',
'rank': 1,
'value': 'Brookhaven National '
'Laboratory'},
{'major': 'N',
'name': 'subject',
'rank': 2,
'value': 'Ozone'},
{'major': 'N',
'name': 'subject',
'rank': 3,
'value': 'Radiation'},
{'major': 'N',
'name': 'subject',
'rank': 4,
'value': 'Cancer'},
{'major': 'N',
'name': 'subject',
'rank': 5,
'value': 'Research'},
{'major': 'N',
'name': 'subject',
'rank': 6,
'value': 'Fish and Other Marine Life'}],
'lead_paragraph': 'RESEARCHERS at the Brookhaven '
'National Laboratory are employing a '
'novel model to study skin cancer in '
'humans: they are exposing tiny '
'tropical fish to ultraviolet '
'radiation.',
'multimedia': [],
'news_desk': 'Science Desk',
'print_page': '3',
'print_section': 'C',
'pub_date': '1989-12-26T05:00:00+0000',
'section_name': 'Science',
'snippet': '',
'source': 'The New York Times',
'type_of_material': 'News',
'uri': 'nyt://article/e3e5e5e5-1b32-5e2b-aea7-cf20c558dbd3',
'web_url': 'https://www.nytimes.com/1989/12/26/science/tiny-fish-help-solve-cancer-riddle.html',
'word_count': 870},
{'_id': 'nyt://article/32a2431d-623a-525b-a21d-d401be865818',
'abstract': 'LEAD: Clouds, even the ones formed by '
…这样继续下去,时间太长,无法在这里全部展示
现在,当我只想列出一个标题时,我使用:
pprint(articles['response']['docs'][0]['headline']['print_headline'])
我得到了输出
'Tiny Fish Help Solve Cancer Riddle'
问题是当我想从这个JSON对象中挑出所有标题,并列出它们时。我试过:
index = 0
for headline in articles:
headlineslist = ['response']['docs'][index]['headline']['print_headline'].split("''")
index = index + 1
headlineslist
但是我得到了错误TypeError:列表索引必须是整数或片,而不是str
换句话说,当我在索引[0]中“列出”一个标题时,它就起作用了,但当我试图在每个索引上重复这个过程时,它就不起作用了。如何遍历每个索引以获得与第一个索引类似的输出列表?要遍历文档列表,只需执行以下操作:
for doc in (articles['response']['docs']):
print(doc['headline']['print_headline'])
这将打印所有标题 我不清楚为什么你认为
文章的标题:
会在列表中重复文章[“回复”][“文档”]
。你期望[“回复”][“文档”]
要计算的对象?您没有在文章标题的循环中引用变量headline
。请提供整个错误消息。啊,我明白了。概括来说:你是说变量索引之前的所有内容都是列表本身,变量索引之后的所有内容都是单独的行项目吗?articles['response']['docs']
返回一个列表。文档的在…
中迭代此列表。每一项都是一本字典。然后我们提取键“headline”的值。这个值也是一个字典,在这里我们为键“print_headline”提取所需的字符串值。