[英]how do i make a nested dictionary in python?
I'm trying to make an API that fetches memes from Reddit and I am stuck at creating JSON data with nested dictionaries.我正在尝试制作一个从 Reddit 获取模因的 API,但我一直坚持使用嵌套字典创建 JSON 数据。 I have the following dictionaries and I want to merge them together.
我有以下字典,我想将它们合并在一起。 How do I go about it?
我该怎么做 go 呢?
{
"author": "Mizanur1214",
"nsfw": false,
"subreddit": "dankmemes",
"title": "They do be like that",
"upvotes": 33302,
"url": "https://i.imgur.com/xZpJFQU.jpg"
}
and the other one as follows另一个如下
{
"author": "Kristis1",
"nsfw": false,
"subreddit": "me_irl",
"title": "me irl",
"upvotes": 1941,
"url": "https://i.redd.it/u2qesppixhh61.jpg"
}
I want to make something like the following in python我想在 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"
}
}
As others pointed out the result is not a valid json or dict.正如其他人指出的那样,结果不是有效的 json 或字典。
You need a key for the dict
object:您需要
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: 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.