<menu id="ycqsw"></menu><nav id="ycqsw"><code id="ycqsw"></code></nav>
<dd id="ycqsw"><menu id="ycqsw"></menu></dd>
  • <nav id="ycqsw"></nav>
    <menu id="ycqsw"><strong id="ycqsw"></strong></menu>
    <xmp id="ycqsw"><nav id="ycqsw"></nav>
  • python解析json文件并提?。╬ython處理excel教程實例)


    在測試工作中,通常都會接觸到期望結果數據與實際結果數據一致性比對的測試場景,對于復雜、龐大數據的比對工作。如果依靠人工執行,其成本相當高,難以保障執行結果的一致性(多次執行可能存在偏差),并且重復性極高。因此,通常我們需要考慮如何通過自動化工具實現數據的比對。

    本文分享下如何實現Json、字典數據結構的遞歸解析。


    JSON的兩種數據結構

    1.Key-Value對集合,在Python語言中可以理解為字典(Dict),如下圖。

    Python 實現JSON、字典數據結構的遞歸解析

    2.有序集合,在Python語言中可以理解為列表(List),如下圖。

    Python 實現JSON、字典數據結構的遞歸解析

    代碼示例

    我們以下面這個較為復雜的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:查詢
    

    版權聲明:本文內容由互聯網用戶自發貢獻,該文觀點僅代表作者本人。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。如發現本站有涉嫌抄襲侵權/違法違規的內容, 請發送郵件至 舉報,一經查實,本站將立刻刪除。

    發表評論

    登錄后才能評論
    国产精品区一区二区免费