2016-07-29 28 views
0
header = {'Content-type': 'application/json','Authorization': 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' } 
url = 'https://sandbox-authservice.priaid.ch/login' 
response = requests.post(url, headers = header, verify=False).json() 
token = json.dumps(response) 
print token['ValidThrough'] 

我想在我的webhook中打印ValidThrough属性,该属性通过POST调用作为JSON数据接收。我知道这已经在这里被问了很多次,但是打印的令牌['ValidThrough']不适用于我。我收到错误“TypeError:字符串索引必须是整数,而不是str”在Python中访问JSON数据

+0

是的,它也返回值,它有两个属性,'Token'和'ValidThrough' –

回答

2

由于响应已经看起来在json中,没有必要使用json.dumps

json.dumps在字典上将返回一个字符串,它不能被明确索引,因此,该错误。

0

a请求响应.json()方法已经将字符串的内容加载到json。

你应该使用它,但是你的代码稍后将其序列化为一个字符串,因此错误(token是你所期待的字典的字符串表示,而不是字典)。你应该只省略json.dumps(response)线,并使用response['ValidThrough']

这里还有一个错误,即使你假设.json()返回应该重新去系列化,你应该已经使用json.loads(response),以将其加载到字典(字符串不会转储到序列化)