json对象判断相等
支持dict、list、普通数据类型对象比较
def compare(src, des):
if type(src) != type(des):
return False
if isinstance(src, (str, int, float)):
if src == des:
return True
else:
return False
elif isinstance(src, dict):
if len(src) != len(des):
return False
keys = set(list(src.keys()) + list(des.keys()))
for key in keys:
if key not in src or key not in des:
return False
result = compare(src[key], des[key])
if not result:
return result
return True
elif isinstance(src, list):
if len(src) != len(des):
return False
length = len(src)
for i in range(length):
for lj in des:
result = compare(src[i], lj)
if result:
des.remove(lj) # des列表中的元素匹配上后,des.remove掉元素, len(des)会-1;
break
length -= 1
if length != len(des):
return False
return True