我有下面的回复消息和我遵循的代码结果= a["{\"msg\":\"result\",\"id\":\"test\",\"result\":{\"categories\":[\"<20M\" ,\"20M+\",\"50M+\",\"300M+\",\"1G\"],\"系列\":[{\"名称\":\"光纤\",\"数据\ ":[216,41,10393,37394,6016]}],\"总数\":{\"<20M\":216,\"20M+\":41,\"50M+\":10393,\" 300M+\":37394,\"1G\":6016}}}"]#Remove a sliceresult=result[1:] print('Result After slice: {}'.format(sliceresult)) string_input = ''.join(str(s) for s in sliceresult) print(string_input) category = json.loads(string_input)['result']['categories'] series = json.loads(string_input)['result']['series'] total = json.loads(string_input)['result']['totals'] print(category, series, total) Error Response was: category = json.loads(result[1:])['result']['categories'] TypeError: list indices must be integers, not str
1 回答
哈士奇WWW
TA贡献1799条经验 获得超6个赞
这可能会帮助您开始:
a = ["{\"msg\":\"result\",\"id\":\"test\",\"result\":{\"categories\":[\"<20M\",\"20M+\",\"50M+\",\"300M+\",\"1G\"],\"series\":[{\"name\":\"Fiber\",\"data\":[216,41,10393,37394,6016]}],\"totals\":{\"<20M\":216,\"20M+\":41,\"50M+\":10393,\"300M+\":37394,\"1G\":6016}}}"]
string_input = ''.join(str(s) for s in a)
print(string_input)
category = json.loads(string_input)['result']['categories']
series = json.loads(string_input)['result']['series']
total = json.loads(string_input)['result']['totals']
print(category, series, total)
还要小心,您输入的字符串格式不正确。应该"不是”
添加回答
举报
0/150
提交
取消