我有JSON对象,其中有None值,我想删除任何维护这些值的字典。如果满足条件语句,如何成功实现删除字典的逻辑?
{
"MetaData": {},
"SRData": {
"ListOfLa311BulkyItem": {
"BulkyItem": [
{
"BulkyItemCount": "None",
"BulkyItemType": "None",
"DriverFirstName": "SA",
"DriverLastName": "Aguilar",
"LastUpdatedBy": "SANSTAR1",
"Name": "072420150115103541",
"Type": "Bulky Items"
},
{
"BulkyItemCount": "None",
"BulkyItemType": "None",
"DriverFirstName": "SA",
"DriverLastName": "Aguilar",
"LastUpdatedBy": "SANSTAR1",
"Name": "072420150115103542",
"Type": "Bulky Items"
}
]
},
"ReasonCode": "",
"ResolutionCode": "A",
"SRNumber": "1-21939511"
}
}
下面代码中的 D
引用了BulkyItem列表中的词典。
lIndexes = []
nCnt = len(l311)
for i in range(nCnt):
dd = l311[i]
if(dd==d):
lIndexes.append(i)
for i in lIndexes:
i
#remove the d from the original l311 in a reverse order:
# print ("order (of index) in which items are deleted:" )
for ii in reversed(lIndexes):
if d['Name'] <> str:
# print(ii)
del l311[ii]
使用@ Ewan建议的语法输出JSON。
[
{
"MetaData": {},
"SRData": {
"ListOfLa311MetalHouseholdAppliancesPickup": {
"La311MetalHouseholdAppliancesPickup": [
{
"DriverFirstName": "SA",
"DriverLastName": "Aguilar",
"HouseHoldItemCount": "None",
"HouseholdItem": "None",
"LastUpdatedBy": "SANSTAR1",
"Name": "072820151331438211",
"Type": "Metal/Household Appliances"
},
{
"DriverFirstName": "SA",
"DriverLastName": "Aguilar",
"ElectronicWestType": "None",
"ItemCount": "None",
"LastUpdatedBy": "SANSTAR1",
"Name": " ",
"Type": "Metal/Household Appliances"
}
]
},
"ReasonCode": "",
"ResolutionCode": "",
"SRNumber": "1-22184611"
}
}
]
代码除了上一个字典附加外,还有什么可能导致这个?
答案 0 :(得分:1)
看起来JSON中的None
是字符串而不是None
类型。
因此,您需要将支票修改为:
if d["BulkyItemCount"] == None or d["BulkyItemCount"] == "None"