python访问json中的value错误(TypeError: string indices must be integers, not str)
出现问题:远程请求获取用户聊天信息,服务器返回若干结果,此时返回结果如下:
data
{ "total":7, "start":0, "count":1, "messages": [{"from_type":"admin","from_id":"david1","create_time":1551363779,"from_appkey":"43ec274cae31352d33f03646","target_type":"single","msg_body":{"extras":{},"text":"hahaha"},"msg_type":"text","from_platform":"api","target_id":"david1","sui_mtime":1551363382,"version":1,"msgid": 2884707847,"msg_ctime":1551363779240,"msg_level": 0} ]}
调用data['total']出现TypeError: string indices must be integers, not str
解决方法:
new_data = json.loads(data)
new_data
{u'count': 1, u'start': 0, u'total': 7, u'messages': [{u'sui_mtime': 1551363382, u'version': 1, u'msg_type': u'text', u'msgid': 2884707847L, u'from_platform': u'api', u'target_id': u'david1', u'target_type': u'single', u'from_type': u'admin', u'msg_ctime': 1551363779240L, u'msg_level': 0, u'msg_body': {u'text': u'hahaha', u'extras': {}}, u'create_time': 1551363779, u'from_appkey': u'43ec274cae31352d33f03646', u'from_id': u'david1'}]}
此时可以使用data['total']
本次错误:使用了不规范的key,需要转换!!!
更多推荐
所有评论(0)