My json file look likes this and I'm trying to access the element syslog
in a for loop.
{
"cleanup":{
"folderpath":"/home/FBML7HR/logs",
"logfilename":""
},
"preparation":{
"configuration":{
"src_configfile":"src.cfg",
"dest_configfile":"/var/home/FBML7HR/etc/vxn.cfg"
},
"executable_info1":[
{
"login_info":{
"hostname":"10.4.0.xxx",
"username":"***",
"password":"***"
}
},
{
"command":{
"folderpath":"/var/home/FBML7HR/SrcCode/vxnservers/fdchost/north/test/hostsim/",
"processname":"northhostsim",
"parameters":"-d"
}
}
],
"executable_info2":[
{
"login_info":{
"hostname":"10.4.0.xxx",
"username":"***",
"password":"***"
}
},
{
"command":{
"folderpath":"/var/home/FBML7HR/SrcCode/vxnservers/fdchost/north/build/Linux-2.6.18-194.8.1.el5/bin",
"processname":"northhost",
"parameters":"-s brazil -d"
}
}
],
"executable_info3":[
{
"login_info":{
"hostname":"10.4.0.xxx",
"username":"***",
"password":"***"
}
},
{
"command":{
"folderpath":"cd /var/home/xxx/SrcCode/vxnservers/fdchost/north/test/vxnclient_mt",
"processname":"vxnclient_north_mt",
"parameters":"0 320 205 14897 16880 60000 60000 2 2"
}
}
]
},
"execution":[
{
"test_case":{
"scriptname":"/var/home/FBML7HR/test/testcase1.sh",
"testreport":{
"syslog":"/var/log/messages",
"backupsyslog":"backuplogs1.txt",
"clientsimlog":"/var/home/FBML7HR/test/out.log",
"backupclientsimlog":"Clientlogs1.txt"
}
}
},
{
"test_case":{
"scriptname":"/var/home/FBML7HR/test/testcase2.sh",
"testreport":{
"syslog":"/var/log/messages",
"backupsyslog":"backuplogs2.txt",
"clientsimlog":"/var/home/FBML7HR/test/out.log",
"backupclientsimlog":"Clientlogs2.txt"
}
}
}
],
"verification":{
"testreport":{
"syslog":"/var/log/messages",
"backupsyslog":"backuplogs.txt",
"reportfilename":"/var/home/FBML7HR/test/out.log",
"backuplogfile":"Clientlogs.txt"
}
}
}
I do it like this:
for i in data['execution']:
cmd = data['execution'][i]['test_case']['scriptname']
But I get the error saying "TypeError: list indices must be integers, not dict".
I'm new to python (and json as well). Could anybody suggest where I'm going wrong ?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…