我在我的python程序中使用json
temp = json.loads(text2)
data = temp["data"]
try:
ld = data["all"]
globals()['currentbrowserall'] = ld
del data["all"]
except:
s = str(sys.exc_info()[0])
s2 = str(sys.exc_info()[1])
s3 = str(sys.exc_info()[2])
print "%s, %s, %s" % (s, s2, s3)
问题是我一直在"所有"虽然每次Key都在那里,数据以及一切都像它应该的那样,但它会输出错误。为什么?有什么问题吗?
以下是json的样本
{
"data": {
"all": [
{
"focused": false,
"windowId": 469,
"tabs": [
{
"active": false,
"title": "Update Driver Booster",
"url_short": "www.iobit.com"
}
]
}
],
"timeline": [
{
"hostname": "www.youtube.com",
"startStr": "8:31",
"duration": 317,
"endStr": "8:36",
"startDate": {
"hours": 8,
"minutes": "31"
},
"endDate": {
"hours": 8,
"minutes": "36"
}
},
{
"hostname": "stackoverflow.com",
"startStr": "8:50",
"duration": 110,
"endStr": "8:52",
"startDate": {
"hours": 8,
"minutes": "50"
},
"endDate": {
"hours": 8,
"minutes": "52"
}
}
]
}
}
答案 0 :(得分:0)
我确定您的data
字典没有名为all
的属性: -
del
(print data
)之前打印dict进行检查。使用以下内容确保您不会出现异常: -
如果data.has_key(' all'): del data [' all']