文章内容
2020/6/4 16:51:27,作 者: 黄兵
TypeError: string indices must be integers
最近再处理api返回数据的时候出现了如下错误:
TypeError: string indices must be integers
具体代码如下:
def test_sms_content(self): # 首先向数据库中插入数据 test_insert_database() response_404 = self.client.get('/api/v1/sms_content/{}'.format('444')) self.assertEqual(response_404.status_code, 404) self.assertEqual(SMSReceive.get_phone_number('4433908876')['id'], 1) response_200 = self.client.get('/api/v1/sms_content/{}'.format('4433908876')) json_response = response_200.get_data(as_text=True) print(json_response) self.assertEqual(response_200.status_code, 200) self.assertEqual(json_response['count'], 1)
出现问题的原因:
主要是对返回的数据没有进行json编码。
解决方案:
对返回的数据进行json编码,在第8行,对返回内容json编码:
json_response = json.loads(response_200.get_data(as_text=True))
问题解决。
黄兵个人博客原创。
评论列表