【问题标题】:python, Json and string indices must be integers, not strpython、Json 和字符串索引必须是整数,而不是 str
【发布时间】:2012-05-10 13:53:18
【问题描述】:

我正在使用 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

我该怎么做?

谢谢

【问题讨论】:

    标签: python json url httplib2


    【解决方案1】:

    你可以使用转储;

    result = json.dumps(content)
    
    result = json.loads(result)
    

    【讨论】:

      【解决方案2】:

      如果响应类型是 json 并且它的类型是 str。

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

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

      那就把它当作字典吧。

      accesstoken = retdict['access_token']
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-08-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-12-16
        相关资源
        最近更新 更多