Blueherb In solitude, where we are least alone 王佳鑫

高老师好

点击查看代码
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

数据样例:
{ "TaskID":"f48455063cfc4fc4a6341ce6cfbf6e32", "AssetIPInfo":[ { "Ip":"110.6.18.98", "IpType":0, "OpenPort":[ { "port":8080, "protocol":"tcp" } ] } ], "AssetName":"oneneb站点", "AssetType":"0699", "AssetTag":"one站点", "AssetInfo":[ { "Brand":[ { "Manufacturer":"联网", "Name":"联网", "Region":"1" } ], "Chip":[ { "Manufacturer":"", "Model":"" } ], "CPU":[ { "Manufacturer":"", "Model":"" } ], "HardwareEnvironment":"x86", "information":[ { "Model":"onenb站点", "Version":"web站点" } ], "Language":"simplified chinese", "Memory":[ { "Manufacturer":"1", "Model":"" } ], "OpenSource":0, "SoftwareEnvironment":"linux" } ], "IsAccess":0, "State":1, "ObjectName":"中国用开放平台", "DeviceLevel":"1", "Reserve":[ ] }

posted @ 2024-12-26 10:07  阿呆学习之路  阅读(14)  评论(1)    收藏  举报