Mei*_*eir 5 python url json httplib2
我使用的是Python,我向URL发送了一个请求,并使用httplib2收到了回复.我得到的答复是在JSon中,我如何访问特定的参数.我现在所拥有的是:
resp, content = parser.request(access_token_uri, method = 'POST', body = params, headers = headers)
raise Exception(content['access_token'])
Run Code Online (Sandbox Code Playgroud)
我得到了错误
string indices must be integers, not str
Run Code Online (Sandbox Code Playgroud)
我该怎么做?
谢谢
好吧,如果响应类型是json,它来自类型str.
如果你运行2.4的Python使用simplejson,如果2.6使用json:
import json
# Your code
retdict = json.loads(content)
Run Code Online (Sandbox Code Playgroud)
然后像对待字典一样对待它.
accesstoken = retdict['access_token']
Run Code Online (Sandbox Code Playgroud)