在提问之前,我想说我没有经验,还在学习中。
我需要一个嵌套可拖拽的树状结构,并找到了这个适用于vuejs的he-he-tree,它完全符合我的需求:https://he-tree-vue.phphe.com/
我有一个Python / Flask API,可以提供数据来为vue中的树形菜单提供数据。
问题在于he-he-tree使用js数组,看起来像这样:
treeData: [{text: 'node 1'}, {text: 'node 2', children: [{text: 'node 2-1'}]}, {text: 'node 3'}, {text: 'node 4'}, {text: 'node 5'}, {text: 'node 6'}]
但是Python不允许字典具有重复的键。
基本上,我正在尝试在Flask中发布数据的方法如下:
def rawdata():
data = {
"text": "node1",
"text": "node2",
"text": "node3",
}
return make_response(jsonify(data), 200)
尽管 Python 字典不允许重复键,但它只会保留最后一个。我在类似问题的一些讨论中找到了这个链接:Make a dictionary with duplicate keys in Python,但对我的情况并没有太大帮助...有什么解决方法可以帮助我吗?谢谢!RESOLVED:python_user 在这个问题上发表了评论,然后删除了他的评论,但那解决了我的问题。我还是太菜了。他建议我在 JS 代码中使用字典列表,在我的 Python 代码中使用简单字典,这基本上就是问题所在。将我的代码转换为:
def rawdata():
data = [
{"text" : "node1"},
{"text" : "node2"},
{"text" : "node3"}
]
return make_response(jsonify(data), 200)
正常运作。
谢谢!