有没有办法用python列出json中未知数量的元素?

2024-09-26 18:16:14 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我试图在json中列出元素,但我不知道会有多少,这取决于标记。你知道吗

我试过这个:

tag = "cat"
r = requests.get('https://danbooru.donmai.us/related_tag.json?query=' + tag)
j = json.loads(r.text)
dump = json.dumps(j, indent=2, sort_keys=True)
taglist = []
taglist.append(j["wiki_page_tags"][all][0])
if tag in dump:
    print(f"{taglist}")

我想得到这样的结果

tag1
tag2
tag3

对不起,你的垃圾代码,谢谢你的帮助


Tags: https标记json元素gettagqueryrequests
1条回答
网友
1楼 · 发布于 2024-09-26 18:16:14

这就是你要找的吗?你知道吗

print('\n'.join([x[0] for x in j["wiki_page_tags"]]))

完整示例:

import requests
import json

tag = "cat"
r = requests.get('https://danbooru.donmai.us/related_tag.json?query=' + tag)
j = json.loads(r.text)
dump = json.dumps(j, indent=2, sort_keys=True)
taglist = [x[0] for x in j["wiki_page_tags"]]
print('\n'.join(taglist))

印刷品:

stuffed_animal
cathead
cheetah
leopard
lion
panther
serval
tiger
black_cat
white_cat
calico
siamese_cat
kitten
cat_focus
too_many_cats

相关问题 更多 >

    热门问题