我有下面显示的JSON结构和下面显示的python代码来操作JSON结构。
import json
json_input = {
"msgType": "0",
"tid": "1",
"data": "[{\"Severity\":\"warn\",\"Subject\":\"Reporting \",\"Message\":\"tdetails:{\\\"Product\\\":\\\"Gecko\\\",\\\"CPUs\\\":8,\\\"Language\\\":\\\"en-GB\\\",\\\"isEvent\\\":\\\">
"Timestamp": "1432703193431",
"Host": "myserver.com",
"Agent": "Experience",
"AppName": "undefined",
"AppInstance": "my_server",
"Group": "UndefinedGroup"
}
data = json_input['data']
tdetails = data['Message']
print('json_input {} \n\ndata {} \n\n tdetails {}\n\n'.format(json_input,data,tdetails))
我收到了错误。
Traceback (most recent call last):
File "test_json.py", line 19, in <module>
tdetails = data['Message']
TypeError: string indices must be integers, not str
我希望能够访问data
内的不同字段,例如severity
,subject
以及tdetails
内的字段,例如CPUs
和{ {1}}。我该怎么做?