蟒蛇,JSON和字符串索引必须是整数,而不是str的(python, Json and string

2019-06-24 01:54发布

我使用Python和我发送到URL的请求并获得使用httplib2的答复。 我得到的答复是在JSON,我怎么访问特定的参数。 我目前所面对的是:

resp, content = parser.request(access_token_uri, method = 'POST', body = params, headers =     headers)
raise Exception(content['access_token'])

我得到的错误

string indices must be integers, not str

我该怎么做?

谢谢

Answer 1:

如果响应类型以及JSON和它有类型海峡。

如果你正在运行的Python如果使用simplejson 2.6使用JSON的2.4:

import json
# Your code
  retdict = json.loads(content)

然后把它像一本字典。

accesstoken = retdict['access_token']


Answer 2:

您可以使用dump;

result = json.dumps(content)

result = json.loads(result)


文章来源: python, Json and string indices must be integers, not str