如何在python中创建一个嵌套字典?



我正试图制作一个从Reddit获取模因的API,我被困在用嵌套字典创建JSON数据。我有以下几本字典,我想把它们合并在一起。我该怎么做呢?

{
"author": "Mizanur1214",
"nsfw": false,
"subreddit": "dankmemes",
"title": "They do be like that",
"upvotes": 33302,
"url": "https://i.imgur.com/xZpJFQU.jpg"
}

,另一个如下

{
"author": "Kristis1",
"nsfw": false,
"subreddit": "me_irl",
"title": "me irl",
"upvotes": 1941,
"url": "https://i.redd.it/u2qesppixhh61.jpg"
}

我想在python中编写如下代码

{
{
"author": "Mizanur1214",
"nsfw": false,
"subreddit": "dankmemes",
"title": "They do be like that",
"upvotes": 33302,
"url": "https://i.imgur.com/xZpJFQU.jpg"
},
{
"author": "Kristis1",
"nsfw": false,
"subreddit": "me_irl",
"title": "me irl",
"upvotes": 1941,
"url": "https://i.redd.it/u2qesppixhh61.jpg"
}
}

正如其他人指出的那样,结果不是有效的json或字典。

您需要一个dict对象的键:

a = {
"author": "Mizanur1214",
"nsfw": false,
"subreddit": "dankmemes",
"title": "They do be like that",
"upvotes": 33302,
"url": "https://i.imgur.com/xZpJFQU.jpg"
}
b = {
"author": "Kristis1",
"nsfw": false,
"subreddit": "me_irl",
"title": "me irl",
"upvotes": 1941,
"url": "https://i.redd.it/u2qesppixhh61.jpg"
}
res = {"items": [a, b]}
print(res)

输出:

{'items': [{'author': 'Mizanur1214', 'nsfw': None, 'subreddit': 'dankmemes', 'title': 'They do be like that', 'upvotes': 33302, 'url': 'https://i.imgur.com/xZpJFQU.jpg'}, {'author': 'Kristis1', 'nsfw': None, 'subreddit': 'me_irl', 'title': 'me irl', 'upvotes': 1941, 'url': 'https://i.redd.it/u2qesppixhh61.jpg'}]}

相关内容

  • 没有找到相关文章

最新更新