我想从下面提到的json对象中提取特定值。但是它给出了一些错误
>>> print json
{
"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":
"permalink": "https://www.virustotal.com/file/52d3df0ed60c46f336c131bf2ca454f73bafdc4b04dfa2aea80746f5ba9e6d1c/analysis/1273894724/"
等...... }
实现上述我已经编写了以下代码:
data = json.loads(json_post)
response_dict = simplejson.loads(json_post)
logging.info(response_dict)
if response_dict['response_code'] == 0:
self.response.out.write("11")
elif response_dict['response_code'] == -2:
self.response.out.write("API Request Rate Limit Exceeded<br/>")
elif response_dict['response_code'] == -1:
self.response.out.write("API Key provided is wrong<br/>")
elif response_dict['response_code'] == 1:
link.req_timestamp = response_dict['scans'][0]
Fire_fox=response_dict.get("scans")[1].get("Firefox")
if Fire_fox:
link.firefox = data['scans'][1]['Firefox']
Trend_Micro=response_dict.get("scans")[1].get("TrendMicro")
if Trend_Micro:
link.trendmicro = data['scans'][1]['TrendMicro']
Phishtank=response_dict.get("scans")[1].get("Phishtank")
if Phishtank:
link.phishtank = data['scans'][1]['Phishtank']
错误是KeyError:'扫描'..请告诉我哪里出错了
答案 0 :(得分:1)
您应该检查http://docs.python.org/2/tutorial/datastructures.html
>>> response_dict.has_key('scans')
True
>>> response_dict["scans"].has_key(1)
False
>>> response_dict["scans"].get(1, "Not Found")
'Not Found'
>>> response_dict["scans"][1]
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
response_dict["scans"][1]
KeyError: 1
我不知道您希望从代码中做什么,但这些代码段可能对您有所帮助:
for key in response_dict["scans"].keys():
setattr(link, key.lower().replace("-","_"), response_dict["scans"][key])
或者:
for key in ["Firefox", "TrendMicro", "Phishtank", "McAfee"]:
if scans.has_key(key):
setattr(link, key.lower(), response_dict["scans"][key])
字典非常有用:
responses = {0:"11",
-1:"API Key provided is wrong",
-2:"API Request Rate Limit Exceeded<br/>"}
code = response_dict['response_code']
if responses.has_key(code):
self.response.out.write(responses[code])
答案 1 :(得分:0)
鉴于您正在使用的json结构的复杂性,我更倾向于使用一个库,这样可以更容易地从此结构中获取数据。
例如,请查看https://pypi.python.org/pypi/jsonpath-rw/1.1.2。
此外,您似乎经常犯同样的错误(在您发布的其他2个问题中),您尝试使用密钥访问列表等等....您需要掌握基本的编程技巧,并了解您正在使用的数据结构是什么。