比较两个json是否相等


# 比较两个json是否相等
def equal_json(json1, json2, key):
    if isinstance(json1, dict):
        for key1 in json2:
            if key1 not in json1:
                return "json1不存在%s这个key" % key1
        for key2 in json1:
            if key2 in json2:
                dict_result = cmp_json(json1[key2], json2[key2], key2)
                if dict_result != "true":
                    return dict_result
            else:
                return "json2中不存在%s这个key" % key2
    elif isinstance(json1, list):
        """若为list格式"""
        result = cmp_json_array(json1, json2, key)
        if result != "true":
            return result
    else:
        if str(json1) != str(json2):
            return key + "不相等:" + str(json1) + ":" + str(json2)
    return "true"


posted @ 2021-11-10 17:07  大海一个人听  阅读(997)  评论(0编辑  收藏  举报