亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

是否有任何 JSON 標記篩選示例?

是否有任何 JSON 標記篩選示例?

哈士奇WWW 2022-09-13 19:55:36
我有一個json文件,我需要列出所有數據的所有“自文本”元素。任何例子?數據示例{   "data": [    {              "selftext": "hello there",        "textex": true,   },
查看完整描述

1 回答

?
至尊寶的傳說

TA貢獻1789條經驗 獲得超10個贊

如果您希望能夠在任意級別從任意 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']


查看完整回答
反對 回復 2022-09-13
  • 1 回答
  • 0 關注
  • 96 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號