如何使用python读取Json数据?

我有下面的回复消息和我遵循的代码


结果= 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


墨色风雨
浏览 72回答 1
1回答

哈士奇WWW

这可能会帮助您开始:&nbsp;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)&nbsp; &nbsp; category = json.loads(string_input)['result']['categories']&nbsp; &nbsp; series = json.loads(string_input)['result']['series']&nbsp; &nbsp; total = json.loads(string_input)['result']['totals']&nbsp; &nbsp; print(category, series, total)还要小心,您输入的字符串格式不正确。应该"不是”
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python