由于 python 字典不允许重复键,我试图找到一种将多个键和值添加到 json 文件的方法。
我试图将 dict 转换为 str 但这无济于事,因为我无法附加/更新 str。
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})
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句