Python 如何向json添加重复键?
由于python字典不允许重复键,所以我试图找到一种向json文件添加多个键和值的方法 我已经尝试将dict转换为str,但这没有帮助,因为我无法追加/更新strPython 如何向json添加重复键?,python,Python,由于python字典不允许重复键,所以我试图找到一种向json文件添加多个键和值的方法 我已经尝试将dict转换为str,但这没有帮助,因为我无法追加/更新str numbers = ['one', 'two', 'three'] msg = { "type": "message", "attachments": [ { "contentType": "application/vnd.microsoft.card.adaptive", "conten
numbers = ['one', 'two', 'three']
msg = {
"type": "message",
"attachments": [
{
"contentType": "application/vnd.microsoft.card.adaptive",
"content": {
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
"type": "AdaptiveCard",
"version": "1.0",
"body": [
{
"type": "TextBlock",
"text": "Select the number",
"wrap": True
},
{
"type": "Input.ChoiceSet",
"placeholder": "",
"choices": [
{
"title":"",
"value":""
}
],
"separator": True,
"wrap": True
}
],
"actions": [
{
"type": "Action.Submit",
"title": "Submit"
}
]
}
}
],
"serviceUrl": "https://smba.trafficmanager.net/amer/"
}
for items in numbers:
msg['attachments'][0]['content']['body'][1]['choices'][0].update({"title": items, "value": items})
print(msg['attachments'][0]['content']['body'][1]['choices'][0])
我得到的输出是
{'title':'three','value':'three'}
但是我想看看{'title':'one','value':'one'},{'title':'two','value':'two'},{'title':'three','value':'three'}
这里需要一个字典列表,而不是一个有重复键的字典:
choices = msg['attachments'][0]['content']['body'][1]['choices']
for items in numbers:
choices.append({'title': items, 'value': items})
那不是字典;这是一个字典列表。重复的键违反了JSON/字典的用途。当您想要提取标题值时会发生什么?应该买哪双?首先要说明的是标题键的用途是什么?如果不是唯一的,你需要找到一个唯一的值。自适应卡的工作方式是你必须提供标题和值对。因为这是一个下拉菜单,用户可以在其中选择正确的选项,所以所有的选项都应该显示出来。@TomCider所以您需要一个字典列表,而不是一个多次使用同一键的字典。太棒了。非常感谢。