[英]how do i make a nested dictionary in python?
我正在尝试制作一个从 Reddit 获取模因的 API,但我一直坚持使用嵌套字典创建 JSON 数据。 我有以下字典,我想将它们合并在一起。 我该怎么做 go 呢?
{
"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
object 的密钥:
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)
Output:
{'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'}]}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.