【问题标题】:GAE Python - Accessing values in Response objectGAE Python - 访问响应对象中的值
【发布时间】:2015-02-25 16:26:43
【问题描述】:

我从一个 API 获得响应,其中 response.text 如下所示:

{
  "message": "Queued. Thank you.",
  "id": "<emailID@somedomain.com>"
}

我想访问 ID,为此我使用以下代码:

response.text['id']

但是这会导致以下错误:

TypeError: string indices must be integers

但是,如果我获取响应的副本并使用它创建一个普通字典,这将完全按预期工作。我做错了什么?

【问题讨论】:

    标签: python api httprequest httpresponse


    【解决方案1】:

    那是因为response.text 是一个字符串,而且字符串索引必须是整数。

    但是你知道它是一个 JSON 字符串,所以只需转换它:

    import json
    data = json.loads(response.text)
    print data['id']
    

    【讨论】:

      猜你喜欢
      • 2011-05-16
      • 1970-01-01
      • 2019-05-23
      • 1970-01-01
      • 1970-01-01
      • 2019-10-30
      • 1970-01-01
      • 1970-01-01
      • 2017-01-28
      相关资源
      最近更新 更多