python,Json和字符串索引必须是整数,而不是str [英] python, Json and string indices must be integers, not str

查看:265
本文介绍了python,Json和字符串索引必须是整数,而不是str的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Python,我使用httplib2向URL发送了一个请求并收到了回复.我得到的答复是在JSon中,如何访问特定参数.我目前所拥有的是:

I am using Python, and I sent a request to a URL and received a reply using httplib2. The reply I got was in JSon, how do I access a specific parameter. What I have at the moment is:

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

我该怎么做?

谢谢

推荐答案

如果响应类型为json且类型为str,则为

Well if the response type is json and it comes in type str.

如果您运行的是Python 2.4,请使用simplejson,而2.6请使用json:

If you are running 2.4 of Python use simplejson if 2.6 use json:

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

然后将其视为字典.

accesstoken = retdict['access_token']

这篇关于python,Json和字符串索引必须是整数,而不是str的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆