【问题标题】:How to get key values which locate in different level of a nest json?如何获取位于嵌套json不同级别的键值?
【发布时间】:2022-01-01 17:16:01
【问题描述】:

首先,这里是json的sn-p:

{
    "messages":
    [
        {
            "id": 460,
            "type": "message",
            "date": "2022-01-01T15:10:19",
            "text": "hello, ",
        },
        {
            "id": 461,
            "type": "message",
            "date": "2022-01-01T15:10:19",
            "text":
            [
                "this ",
                {
                    "type": "bold",
                    "text": "is"
                },
                " the ",
                {
                    "type": "underline",
                    "text": "code"
                },
                " names."
            ]
        },
        {
            "id": 462,
            "type": "message",
            "date": "2022-01-01T15:43:41",
            "text":
            [
                "But, ",
                {
                    "type": "bold",
                    "text": "list, set and string "
                },
                " are already builtin functions, so using these names is not recommended."
            ]
        }
    ]
}

我想使用 Python 来获取“text”键的所有值,它们位于 json 的不同级别,所以我可以将它们合并成一个句子,如下所示:

你好,这是代号。但是list、set和string已经是内置函数,所以不推荐使用这些名称。

我已经尝试了一些本地 dict 操作,还尝试了 Python 中用于 json 的模块 Jmespath,但我仍然无法找到完成它的方法。所以你可能知道我是 Python 的新手,希望我能有一个易于理解的解决方案来实现我的目标。

谢谢。

【问题讨论】:

    标签: python json


    【解决方案1】:

    您可以处理可以找到dict,list,str 的不同数据类型并应用良好的逻辑

    • dict : 如果 value 是一个容器 : 在里面搜索
    • list : 搜索它的所有元素
    • str :添加它
    def search(data, search_key) -> str:
        r = ''
        if isinstance(data, dict):
            for key, value in data.items():
                if isinstance(value, (dict, list)):
                    r += search(value, search_key)
                if isinstance(value, str) and key == search_key:
                    r += value
    
        if isinstance(data, list):
            for item in data:
                r += search(item, search_key)
    
        if isinstance(data, str):
            r += data
    
        return r
    
    v = {"messages": [
        {"id": 460, "type": "message", "date": "2022-01-01T15:10:19", "text": "hello, "},
        {"id": 461, "type": "message", "date": "2022-01-01T15:10:19",
         "text": ["this ", {"type": "bold", "text": "is"}, " the ",
                  {"type": "underline", "text": "code"}, " names."]},
        {"id": 462, "type": "message", "date": "2022-01-01T15:43:41",
         "text": ["But, ", {"type": "bold", "text": "list, set and string "},
                  " are already builtin functions, so using these names is not recommended."]}
    ]}
    
    result = search(v, 'text')
    print(result)
    # hello, this is the code names.But, list, set and string  are already builtin functions, so using these names is not recommended.
    

    【讨论】:

    • 非常感谢您,工作和整洁!加上很好的解释。我不知道函数可以在内部调用自己
    • @user3127293 称为“递归”
    猜你喜欢
    • 1970-01-01
    • 2021-01-03
    • 1970-01-01
    • 2022-10-16
    • 2016-08-19
    • 2015-08-10
    • 1970-01-01
    • 2021-04-04
    • 1970-01-01
    相关资源
    最近更新 更多