【发布时间】: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 的新手,希望我能有一个易于理解的解决方案来实现我的目标。
谢谢。
【问题讨论】: