高老師好
點(diǎn)擊查看代碼
def get_nested_value(data, keys, values):
dataCopy = data
if not keys:
return False
foundEnd = False
for key, value in zip(keys, values):
print(key, value)
subkeys = key.split('.')
foundMid = False
for subkey in subkeys:
print("subkey值{}".format(subkey))
print("data值{}".format(dataCopy))
found = False
if dataCopy is None:
return False
# 判斷是否是對(duì)象
if isinstance(dataCopy, dict):
dataCopy = dataCopy.get(subkey)
print("值{}".format(dataCopy))
# 判斷是否是字符串或者數(shù)字,是的話直接比較
if(isinstance(dataCopy, (int, str))):
if str(dataCopy) == value:
foundMid = True
print("單層 {}".format(foundMid))
break
continue
# 判斷是否是數(shù)組,是的話遍歷數(shù)組
elif isinstance(dataCopy, list):
if subkey.isdigit() and int(subkey) < len(dataCopy):
dataCopy = dataCopy[int(subkey)]
else:
print("222")
foundThird = False
for item in dataCopy:
print("item值{}".format(item))
if isinstance(item, dict) and subkey in item:
dataCopy = item[subkey]
print("值{}".format(dataCopy))
if(isinstance(dataCopy, (int, str))):
if str(dataCopy) == value:
foundThird = True
print("多層{}".format(foundThird))
break
print("2221232222222")
continue
print("2221232222")
if isinstance(dataCopy, list):
print("item值{}".format(item))
print("2222222")
if subkey.isdigit() and int(subkey) < len(dataCopy):
dataCopy = dataCopy[int(subkey)]
else:
print("2222")
for item in dataCopy:
if isinstance(item, dict) and subkey in item:
print("33333")
dataCopy = item[subkey]
print("值{}".format(dataCopy))
if(isinstance(dataCopy, (int, str))):
if str(dataCopy) == value:
found = True
print("內(nèi)多層{}".format(found))
break
continue
print("aaaa2221232222")
if not found:
print("not多層{}".format(not found))
break
else:
foundMid = found
if not found:
print("not多層{}".format(not found))
break
else:
foundMid = found
# return False
else:
print("333")
if not found:
print("not單層{}".format(found))
else:
foundMid = found
if not foundMid:
print("not foundMid2 {}".format(foundMid))
foundEnd = False
else:
print("foundMid2 {}".format(foundMid))
foundEnd = True
dataCopy = data
return foundEnd
數(shù)據(jù)樣例:
{ "TaskID":"f48455063cfc4fc4a6341ce6cfbf6e32", "AssetIPInfo":[ { "Ip":"110.6.18.98", "IpType":0, "OpenPort":[ { "port":8080, "protocol":"tcp" } ] } ], "AssetName":"oneneb站點(diǎn)", "AssetType":"0699", "AssetTag":"one站點(diǎn)", "AssetInfo":[ { "Brand":[ { "Manufacturer":"聯(lián)網(wǎng)", "Name":"聯(lián)網(wǎng)", "Region":"1" } ], "Chip":[ { "Manufacturer":"", "Model":"" } ], "CPU":[ { "Manufacturer":"", "Model":"" } ], "HardwareEnvironment":"x86", "information":[ { "Model":"onenb站點(diǎn)", "Version":"web站點(diǎn)" } ], "Language":"simplified chinese", "Memory":[ { "Manufacturer":"1", "Model":"" } ], "OpenSource":0, "SoftwareEnvironment":"linux" } ], "IsAccess":0, "State":1, "ObjectName":"中國(guó)用開(kāi)放平臺(tái)", "DeviceLevel":"1", "Reserve":[ ] }

浙公網(wǎng)安備 33010602011771號(hào)