在测试工作中,通常都会接触到期望结果数据与实际结果数据一致性比对的测试场景,对于复杂、庞大数据的比对工作。如果依靠人工执行,其成本相当高,难以保障执行结果的一致性(多次执行可能存在偏差),并且重复性极高。因此,通常我们需要考虑如何通过自动化工具实现数据的比对。

本文分享下如何实现json、字典数据结构的递归解析。

json的两种数据结构

1.key-value对集合,在python语言中可以理解为字典(dict),如下图。

2.有序集合,在python语言中可以理解为列表(list),如下图。

代码示例

我们以下面这个较为复杂的json为例。

test_json = {
    "code": 200,
    "data": [
        {
            "apps": {
                "app_op_seq": [
                    {
                        "action": "点击",
                        "module_name": "聚划算",
                        "module_type": "resource"
                    }
                ]
            },
            "content": {
                "des": {
                    "company_name": "耐克",
                    "intent": [
                        "full"
                    ]
                },
                "rel": [
                    {
                        "des": {
                            "person_name": "欧阳玖林",
                            "political_status": "金牌会员"
                        },
                        "ont": [
                            "company",
                            "person"
                        ],
                        "relidx": [
                            0,
                            "8-9"
                        ],
                        "relname": "欧阳",
                        "segs": [
                            "耐克篮球鞋"
                        ]
                    }
                ],
                "segs": [
                    "耐克篮球鞋"
                ]
            },
            "content_op": "查询"
        }
    ]
}

实现源码

def json_generator(indict, key_value=none):
    """
    递归解析 json 数据
    :param indict:
    :param key_value:
    :return:
    """
    key_value = key_value[:] if key_value else []
    if isinstance(indict, dict):
        for key, value in indict.items():
            tier = - 1
            if isinstance(value, dict) and value != {}:
                if len(value) == 0:
                    yield key_value + [key, '{}']
                else:
                    for d in json_generator(value, key_value + [key]):
                        yield d
            elif isinstance(value, list) and value != []:
                if len(value) == 0:
                    yield key_value + [key, '[]']
                else:
                    for v in value:
                        tier = tier + 1
                        for d in json_generator(v, key_value + [key + '[{0}]'.format(tier)]):
                            yield d
            else:
                yield key_value + [key, value]
    else:
        if not key_value:
            yield indict
        else:
            yield key_value + [indict]

def structure_flow_sub(json_gen_obj):
    """
    解析结果 格式化输出

    :param json_gen_obj:
    :return: jsonpath:data[0].apps.app_op_seq[0].action
    """
    structure = {}

    for i in json_generator(json_gen_obj):
        if '.'.join(i[:-1]) in structure.keys() and not isinstance(structure['.'.join(i[:-1])], list):
            structure['.'.join(i[:-1])] = [structure['.'.join(i[:-1])]]
            structure['.'.join(i[:-1])].append(i[-1])
        elif '.'.join(i[:-1]) in structure.keys() and isinstance(structure['.'.join(i[:-1])], list):
            structure['.'.join(i[:-1])].append(i[-1])
        else:
            structure['.'.join(i[:-1])] = i[-1]
    return structure

def json_path_parse(json_gen_obj):
    """
    json路径解析
    :param json_gen_obj:
    :return:
    """
    structure_list = []
    if isinstance(json_gen_obj, dict):
        structure = structure_flow_sub(json_gen_obj)
        structure_list.append(structure)
        return structure_list
    if isinstance(json_gen_obj, list):
        for i in json_gen_obj:
            result = structure_flow_sub(i)
            structure_list.append(result)
        return structure_list

if __name__ == '__main__':
    all_leaf_node_dict = json_path_parse(test_json)
    line_number = 0
    for k, v in all_leaf_node_dict[0].items():
        line_number += 1
        print("{line_number}  jsonpath:{json_path}   value:{value} ".format(
            line_number=line_number, json_path=k, value=v))

解析结果如下

1  jsonpath:code   value:200 
2  jsonpath:data[0].apps.app_op_seq[0].action   value:点击 
3  jsonpath:data[0].apps.app_op_seq[0].module_name   value:聚划算 
4  jsonpath:data[0].apps.app_op_seq[0].module_type   value:resource 
5  jsonpath:data[0].content.des.company_name   value:耐克 
6  jsonpath:data[0].content.des.intent[0]   value:full 
7  jsonpath:data[0].content.rel[0].des.person_name   value:欧阳玖林 
8  jsonpath:data[0].content.rel[0].des.political_status   value:金牌会员 
9  jsonpath:data[0].content.rel[0].ont[0]   value:company 
10  jsonpath:data[0].content.rel[0].ont[1]   value:person 
11  jsonpath:data[0].content.rel[0].relidx[0]   value:0 
12  jsonpath:data[0].content.rel[0].relidx[1]   value:8-9 
13  jsonpath:data[0].content.rel[0].relname   value:欧阳 
14  jsonpath:data[0].content.rel[0].segs[0]   value:耐克篮球鞋 
15  jsonpath:data[0].content.segs[0]   value:耐克篮球鞋 
16  jsonpath:data[0].content_op   value:查询