获取字典中嵌套字典 / 列表中某一个字典 key 的值

本贴最后更新于 1616 天前,其中的信息可能已经水流花落

def Dict_get(dicts, objkey, instructions=None,default=None):
tmp = dicts
if isinstance(tmp, dict):
for k, v in tmp.items():
if k == objkey:
return v
else:
if isinstance(v,(dict,list,tuple,set)):
ret = Dict_get(v, objkey, default)
if ret is not default:
logging.info(f'获取{instructions}为:{ret}')
return ret

else:
    if isinstance(tmp, list):
        for k in tmp:
            if k == objkey:
                return [k]
            else:
                if isinstance(k, (dict, list, tuple, set)):
                    ret = Dict_get(k, objkey, default)
                    if ret is not default:
                        logging.info(f'获取{instructions}为:{ret}')
                        return ret
return default

#用于接口自动化测试 ,断言时或获取json结果中某个key的值,根据需要可以改变类型

回帖
请输入回帖内容 ...