是否有任何 JSON 标记筛选示例?

我有一个json文件,我需要列出所有数据的所有“自文本”元素。任何例子?


数据示例


{   "data": [

    {      

        "selftext": "hello there",

        "textex": true,


   },


哈士奇WWW
浏览 97回答 1
1回答

至尊宝的传说

如果您希望能够在任意级别从任意 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']
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python