在 Slack API - Python 中访问嵌套的 Json 键
Accessing Nested Json Keys in Slack API - Python
解析 Python 中的 JSON 响应时遇到问题。我在下面的代码中寻找 return 与 for 循环中的 ID 相关的对象,但收到错误:
TypeError: string indices must be integers
如果我删除数据变量中的'[0]'
TypeError: 'builtin_function_or_method' object is not subscriptable
代码:
import requests
import json
def get_slackusers():
uri = "https://slack.com/api/users.list?token=tokenhere"
try:
response = requests.get(uri)
except requests.exceptions.RequestException as error:
print(error)
data = json.loads(response.text)['members'][0]
for i in data:
if i['id'] == 'slackIDhere':
print[i]
感谢任何帮助。谢谢。
缺少正确的括号,请参阅下面的解决方案。
data = json.loads(response.text)['members']
for i in data:
if i['id'] == 'SlackIDHere':
print([i])
解析 Python 中的 JSON 响应时遇到问题。我在下面的代码中寻找 return 与 for 循环中的 ID 相关的对象,但收到错误:
TypeError: string indices must be integers
如果我删除数据变量中的'[0]'
TypeError: 'builtin_function_or_method' object is not subscriptable
代码:
import requests
import json
def get_slackusers():
uri = "https://slack.com/api/users.list?token=tokenhere"
try:
response = requests.get(uri)
except requests.exceptions.RequestException as error:
print(error)
data = json.loads(response.text)['members'][0]
for i in data:
if i['id'] == 'slackIDhere':
print[i]
感谢任何帮助。谢谢。
缺少正确的括号,请参阅下面的解决方案。
data = json.loads(response.text)['members']
for i in data:
if i['id'] == 'SlackIDHere':
print([i])