使用生成器的方式,嵌套字典所有层级的key值路径
a = { "checksum": "b884cbfb1a6697fa9b9eea9cb2054183", "roots": { "bookmark_bar": { "children": [ { "date_added": "12989159740428363", "id": "4", "name": "test2", "type": "url", "url": "chrome://bookmarks/#1" } ], "date_added": "12989159700896551", "date_modified": "12989159740428363", "id": "1", "name": "bookmark_bar", "type": "folder" }, "other": { "children": [ { "date_added": "12989159740428363", "id": "4", "name": "test", "type": "url", "url": "chrome://bookmarks/#1" } ], "date_added": "12989159700896557", "date_modified": "0", "id": "2", "name": "aaa", "type": "folder" }, "synced": { "children": [ ], "date_added": "12989159700896558", "date_modified": "0", "id": "3", "name": "bbb", "type": "folder" } }, "version": 1 } def traverse(dic, path=None): if not path: path=[] if isinstance(dic,dict): for x in dic.keys(): local_path = path[:] local_path.append(x) for b in traverse(dic[x], local_path): yield b else: yield path,dic for i ,v in traverse(a): print(i,v) if isinstance(v,list): if not v: continue for s,d in traverse(v[0],i): print("s,d:" + str(s),str(d))