用于解码JSON结构的Python程序

时间:2015-05-27 21:11:22

标签: python json

我有下面显示的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内的不同字段,例如severitysubject以及tdetails内的字段,例如CPUs和{ {1}}。我该怎么做?

0 个答案:

没有答案