治疗重新造轮子症系列——递归查询json数据中所有的key值

最近工作中遇到一个需求

判断json数据的结构体是否一致 网上看了很多 发现有个jsondiff 和deepjson两个库好像是 但没法实现我需求 于是自己撸了一个轮子 废话不多说 上代码

import json

'''
封装json对比功能 2020-1-13初建jsondiff类 传入两个对比的json(比对的json, 原始json)实例化这个对象 然后调用check方法返回结果
'''


class JsonDiff:

    def __init__(self, json1, json2):
        self.check_dict = json.loads(json1)
        self.prev_dict = json.loads(json2)
        # self.result = ''
        self.key_list = []
        self.check_list = []
        self.get_dict_allkeys_check(self.check_dict)
        self.get_dict_allkeys_prev(self.prev_dict)

    def get_dict_allkeys_check(self, check_dict):
        """
        多维/嵌套字典数据无限遍历,获取json返回字典的所有key值集合
        :param dict_a:
        :return: key_list
        """

        if isinstance(check_dict, dict):  # 使用isinstance检测数据类型
            for x in range(len(check_dict)):
                # print(dict_a.keys(), type(dict_a.keys()))
                temp_key = list(check_dict.keys())[x]
                temp_value = check_dict[temp_key]
                self.key_list.append(temp_key)
                self.get_dict_allkeys_check(temp_value)  # 自我调用实现无限遍历
        elif isinstance(check_dict, list):
            for k in check_dict:
                if isinstance(k, dict):
                    for x in range(len(k)):
                        temp_key = list(k.keys())[x]
                        temp_value = k[temp_key]
                        self.key_list.append(temp_key)
                        self.get_dict_allkeys_check(temp_value)
        return self.key_list

    def get_dict_allkeys_prev(self, check_dict):
        """
        多维/嵌套字典数据无限遍历,获取json返回字典的所有key值集合
        :param dict_a:
        :return: key_list
        """

        if isinstance(check_dict, dict):  # 使用isinstance检测数据类型
            for x in range(len(check_dict)):
                # print(dict_a.keys(), type(dict_a.keys()))
                temp_key = list(check_dict.keys())[x]
                temp_value = check_dict[temp_key]
                self.check_list.append(temp_key)
                self.get_dict_allkeys_prev(temp_value)  # 自我调用实现无限遍历
        elif isinstance(check_dict, list):
            for k in check_dict:
                if isinstance(k, dict):
                    for x in range(len(k)):
                        temp_key = list(k.keys())[x]
                        temp_value = k[temp_key]
                        self.check_list.append(temp_key)
                        self.get_dict_allkeys_prev(temp_value)
        return self.check_list

    def check(self):
        if len(self.key_list) != len(self.check_list):
            return f'长度匹配失败,requests返回body的key数量{len(self.key_list)},比较的返回值的key数量{len(self.check_list)}'
        for i in range(len(self.key_list)):
            if self.key_list[i] != self.check_list[i]:
                return f'内容匹配失败,返回body的第{i+1}个key的值{self.key_list[i]}
' 
                       f'与比较的返回值的第{i+1}个key的值{self.check_list[i]}不同'
        return 'key值匹配完全正确'


# if __name__ == '__main__':
#     jsondiff = JsonDiff(json_str, json_str1)
#     print(jsondiff.check())

直接实例化对象 传如 比对json和原始json 即可比对key值是否相同 从而得出结构体是否相同

原文地址:https://www.cnblogs.com/lyoko1996/p/12189192.html