点击查看代码
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
# 判断是否是对象
if isinstance(dataCopy, dict):
dataCopy = dataCopy.get(subkey)
print("值{}".format(dataCopy))
# 判断是否是字符串或者数字,是的话直接比较
if(isinstance(dataCopy, (int, str))):
if str(dataCopy) == value:
foundMid = True
print("单层 {}".format(foundMid))
break
continue
# 判断是否是数组,是的话遍历数组
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("内多层{}".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