GAE Python - 访问Response对象中的值

时间:2015-02-25 16:26:44

标签: python api httprequest httpresponse

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

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

我想访问ID,我正在使用以下代码:

response.text['id']

然而,这会导致以下错误:

TypeError: string indices must be integers

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

1 个答案:

答案 0 :(得分:2)

因为response.text是一个字符串,所以字符串索引必须是整数。

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

import json
data = json.loads(response.text)
print data['id']
相关问题