至尊宝的传说
如果您希望能够在任意级别从任意 json 中找到密钥,则应使用递归:def findkey(data, key, resul = None): if resul is None: resul=[] # initialize an empty list for the results if isinstance(data, list): # walk down into lists for d in data: findkey(d, key, resul) elif isinstance(data, dict): # dict processing for k,v in data.items(): if (k == key) and isinstance(v, str): # the expected key and a string value? resul.append(v) elif isinstance(v, list) or isinstance(v, dict): findkey(v, key, resul) # recurse if value is a list or a dict return resul例:>>> data = { "data": [ { "selftext": "hello there", "textex": True, }, ]}>>> findkey(data, 'selftext')['hello there']