python中json对象的内部字典解析

python中json对象的内部字典解析,python,json,Python,Json,我有一个列表,叫做filelist=[] 我在其中附加了一些键值对的字典 filelist.append({"url": url, "exename": ename, "filename": fname}] as I loop through a set of files. Later, loads() the below json object to a "json" key: for file in filelist: file["json"] = json.loads(json) 我

我有一个列表,叫做filelist=[] 我在其中附加了一些键值对的字典

filelist.append({"url": url, "exename": ename, "filename": fname}] as I loop through a set of files.
Later, loads() the below json object to a "json" key: 

for file in filelist: file["json"] = json.loads(json)
我可以毫不费力地提取字典值的第一层,即

for files in filelist:
    print files["json"]["response_code"]

>>> 1
然而,我在获取我假设的内部字典时遇到了问题 文件[“json”][“扫描”] 例如:

for files in filelist:
    for avvendors in files["json"]["scans"]
我对提取“检测到的”键非常感兴趣,但python似乎没有将“扫描”键作为字典加载,而是将其存储为字符串。我正在考虑对内部字典进行第二次加载(),看看会发生什么

for stuff in avvendors:
    if stuf["detected"]:
        #do something
我真的不确定这里发生了什么。我知道我可能会找到答案,但了解一下python在内部如何处理json会很好。而且,我要到星期一或星期二,甚至星期三才有机会做这件事,悬念要把我累死了

以下是完整的可读性块:

对于文件列表中的文件: 对于文件[“json”][“扫描]”中的AVF供应商: 如果供应商[“检测到”]: #做些有用的事

{"response_code": 1,
"verbose_msg": "Scan finished, scan information embedded in this object",

"resource": "99017f6eebbac24f351415dd410d522d",
"scan_id": "52d3df0ed60c46f336c131bf2ca454f73bafdc4b04dfa2aea80746f5ba9e6d1c-1273894724",
"md5": "99017f6eebbac24f351415dd410d522d",
"sha1": "4d1740485713a2ab3a4f5822a01f645fe8387f92",
"sha256": "52d3df0ed60c46f336c131bf2ca454f73bafdc4b04dfa2aea80746f5ba9e6d1c",

"scan_date": "2010-05-15 03:38:44",

"positives": 40,
"total": 40,
"scans": {"nProtect": {"detected": true, "version": "2010-05-14.01", "result": "Trojan.Generic.3611249", "update": "20100514"},
"CAT-QuickHeal": {"detected": true, "version": "10.00", "result": "Trojan.VB.acgy", "update": "20100514"},
"McAfee": {"detected": true, "version": "5.400.0.1158", "result": "Generic.dx!rkx", "update": "20100515"},
"TheHacker": {"detected": true, "version": "6.5.2.0.280", "result": "Trojan/VB.gen", "update": "20100514"},
"VirusBuster": {"detected": true, "version": "5.0.27.0", "result": "Trojan.VB.JFDE", "update": "20100514"},
"NOD32": {"detected": true, "version": "5115", "result": "a variant of Win32/Qhost.NTY", "update": "20100514"},
"F-Prot": {"detected": false, "version": "4.5.1.85", "result": null, "update": "20100514"},
"Symantec": {"detected": true, "version": "20101.1.0.89", "result": "Trojan.KillAV", "update": "20100515"},
"Norman": {"detected": true, "version": "6.04.12", "result": "W32/Smalltroj.YFHZ", "update": "20100514"},
"TrendMicro-HouseCall": {"detected": true, "version": "9.120.0.1004", "result": "TROJ_VB.JVJ", "update": "20100515"},
"Avast": {"detected": true, "version": "4.8.1351.0", "result": "Win32:Malware-gen", "update": "20100514"},
"eSafe": {"detected": true, "version": "7.0.17.0", "result": "Win32.TRVB.Acgy", "update": "20100513"},
"ClamAV": {"detected": false, "version": "0.96.0.3-git", "result": null, "update": "20100514"},
"Kaspersky": {"detected": true, "version": "7.0.0.125", "result": "Trojan.Win32.VB.acgy", "update": "20100515"},
"BitDefender": {"detected": true, "version": "7.2", "result": "Trojan.Generic.3611249", "update": "20100515"},
"Comodo": {"detected": true, "version": "4842", "result": "Heur.Suspicious", "update": "20100515"},
"F-Secure": {"detected": true, "version": "9.0.15370.0", "result": "Trojan.Generic.3611249", "update": "20100514"},
"DrWeb": {"detected": true, "version": "5.0.2.03300", "result": "Trojan.Hosts.37", "update": "20100515"},
"AntiVir": {"detected": true, "version": "8.2.1.242", "result": "TR/VB.acgy.1", "update": "20100514"},
"TrendMicro": {"detected": true, "version": "9.120.0.1004", "result": "TROJ_VB.JVJ", "update": "20100514"},
"McAfee-GW-Edition": {"detected": true, "version": "2010.1", "result": "Generic.dx!rkx", "update": "20100515"},
"Sophos": {"detected": true, "version": "4.53.0", "result": "Troj/VBHost-A", "update": "20100515"},
"eTrust-Vet": {"detected": true, "version": "35.2.7490", "result": "Win32/ASuspect.HDBBD", "update": "20100515"},
"Authentium": {"detected": false, "version": "5.2.0.5", "result": null, "update": "20100514"},
"Jiangmin": {"detected": true, "version": "13.0.900", "result": "Trojan/VB.yqh", "update": "20100514"}, [...] }

迭代dict时要小心。它只返回键。如果需要键和值,请使用
.iteritems()
方法(或在Python3中,
.items()
):


在您的例子中,
scans
键的值是一个dict:
“scans”:{“nProtect”:…}

记住,在Python3.x中,这只是
dict.items()
。因此python可能将内部字典作为字典加载,我错误地认为它只是作为字符串加载?
json.loads()
返回Python对象,例如dict的列表。因此,是的,内部字典是一个dict,而不是一个字符串。不过,现在我想起来了,“true”变为true,“antivi”变为u'antivi'等。当在提供的整个混乱中使用print时,谢谢!
for files in filelist:
   for avvendor, stuf in files["json"]["scans"].iteritems():
       if stuf["detected"]:
          #do something