python—traverse nested dictionary to get all key values of json return result

# coding:utf-8
import json

key_list = []
def get_dict_allkeys(dict_a):
    """
    Traverse the nested dictionary to get all the key values ​​of the result returned by json
    :param dict_a:
    :return: key_list
    "" " 
    IF isinstance (dict_a, dict):   # Use isinstance detection data type 
        # If a dictionary is extracted key stored in the key_list 
        for X in Range (len (dict_a)):
            temp_key = list(dict_a.keys())[x]
            temp_value = dict_a[temp_key]
            key_list.append(temp_key)
            get_dict_allkeys (temp_value)   # calls itself unlimited traverse 
    elif isinstance (dict_a, List):
         # If the list type, the list of elements of the traverse, the dictionary type of extract key in accordance with the above method 
        for k in dict_a:
             IF isinstance (k , dict):
                 for x in range (len (k)):
                    temp_key = list(k.keys())[x]
                    temp_value = k[temp_key]
                    key_list.append(temp_key)
                    get_dict_allkeys (temp_value) # Self-call to realize infinite traversal 
    return key_list

if __name__=="__main__":
    data="""{}"""
    data1 = json.loads(data)
    get_keys = get_dict_allkeys(data1)
    print(get_keys)

 

data = """TodayArrival ": false," Tabs ": [{" Tab ":" fold "," Color ":" df3348 "," IsShowOnPic ": false," TagSort ": 10," TagType ": 15," ShortTab ":" Fold "," BorderColor ":" df3348 "," FontColor ":" ffffff "," ImageBanner ":" https://img1.tuhu.org/activity/image/FkNCBNiSKvVWVA-nlaCyvIlWjOmg_w800_h800.png "," TabDescription ": null , "DeliveredPrice": 0}], "DeliveredPriceTab": null, "ImageTabs": null, "OilTabs": null, "RootCategoryName": "Tires", "CouponPrice": null, "ActivityInfo": "Big-brand tires! Spike every day as low as 99 yuan! Popular specifications take turns! Click to view \ u003e \ u003e "," Advertisement ":" National Pride, Free Control "," Weight ": null," DiscountBannerImage ": null," IsShowOnPic ": false," TagSort ": 12," TagType ": 11," ShortTab ":" Gift "," BorderColor ":" df3348 "," FontColor ":" df3348 "," ImageBanner ":" https: // img1.tuhu.org/GiftManage/Pictures/FmnlYzzo8b_1wJgsW0UcmgOuZSU8.png","TabDescription":null,"DeliveredPrice":0}],"DeliveredPriceTab":null,"ImageTabs":null,"OilTabs":null, : "Tires", "CouponPrice": null, "ActivityInfo": "Big-brand tire assembly! Spike every day as low as 99 yuan! Popular specifications take turns! Click to view \ u003e \ u003e "," Advertisement ":" Durable and safe "," Weight ": null," DiscountBannerImage ": null," ProductBannerImage ": null," ProductAttributes ": null," TireExtendInfo ": null, "NodeNo": "1.13472.18679."""
    

 

Guess you like

Origin www.cnblogs.com/erchun/p/12727749.html