简体   繁体   English

python,Json和字符串索引必须是整数,而不是str

[英]python, Json and string indices must be integers, not str

I am using Python, and I sent a request to a URL and received a reply using httplib2. 我使用的是Python,我向URL发送了一个请求,并使用httplib2收到了回复。 The reply I got was in JSon, how do I access a specific parameter. 我得到的答复是在JSon中,我如何访问特定的参数。 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'])

and I get the error 我得到了错误

string indices must be integers, not str

How do I do it? 我该怎么做?

Thanks 谢谢

Well if the response type is json and it comes in type str. 好吧,如果响应类型是json,它来自类型str。

If you are running 2.4 of Python use simplejson if 2.6 use json: 如果你运行2.4的Python使用simplejson,如果2.6使用json:

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

Then treat it like a dictionary. 然后像对待字典一样对待它。

accesstoken = retdict['access_token']

You can use dump; 你可以使用dump;

result = json.dumps(content)

result = json.loads(result)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM