我不完全确定如何使用这个json输出在python中列出它们,我的意思是循环它们并从JSON输出中收集信息?
JSON输出:https://hastebin.com/riroteqiso.json (从Ombi API收集的数据,使用Traktr作为提供者。)
代码段:
request_headers = {'apiKey': pmrs_api_token, 'content-type': 'application/json'}
async with aiohttp.ClientSession() as ses:
async with ses.get(pmrs_full_endpoint, headers=request_headers) as response:
a = await response.json()
for entry in (a['response']):
print(entry)
错误追溯:
Traceback (most recent call last):
File "/home/sm/.local/lib/python3.6/site-packages/discord/ext/commands/core.py", line 50, in wrapped
ret = yield from coro(*args, **kwargs)
File "/home/sm/Programming/Python/Discord-Bots/Plex-bot/cogs/ombi.py", line 85, in populartv
for entry in (a['response']):
TypeError: list indices must be integers or slices, not str
编辑:
通过改变它来解决这个问题,但是现在我还有另一个问题。
async with aiohttp.ClientSession() as ses:
async with ses.get(pmrs_full_endpoint, headers=request_headers) as response:
a = await response.json()
for entry in a:
b = await response.json()
print(type(b)) # Outputs <class 'list'>
title = (b[entry]['title'])
first_aired = (b[entry]['firstAired'])
desc = (b[entry]['overview'])
再次给我一个关于类型的错误......:/
答案 0 :(得分:0)
你应该改变:
for entry in (a['response']):
人:
for entry in a:
答案 1 :(得分:0)
这应该可以解决您的错误。您不需要新的b对象。 &#39;条目&#39;变量将包含列表中的元素,在这种情况下,它将是列表中的字典对象。因此,您只需使用entry['title']
提取值。
async with aiohttp.ClientSession() as ses:
async with ses.get(pmrs_full_endpoint, headers=request_headers) as response:
a = await response.json()
for entry in a:
title = entry['title']
first_aired = entry['firstAired']
desc = entry['overview']