建立动态树图

时间:2018-09-04 12:55:02

标签: javascript json

早上好,

我一直在研究一个小型项目,以构建动态的treant.js树形图。为此,我使用JSON方法将代码基于可折叠示例。

不幸的是,脚本中的JSON并非完美的JSON,这使我的生活特别困难。

我已经编写了一段脚本,该脚本创建了所需的JSON作为字符串,当我将其写入窗口并将其复制到collapsable.js中时,该图表将被完美绘制。

在这里可以看到一个例子;

{chart: {container: "#collapsable-example",animateOnInit: true,node: {collapsable: true},animation: {nodeAnimation: "easeOutBounce",nodeSpeed: 700,connectorsAnimation: "bounce",connectorsSpeed: 700}},nodeStructure: { "id": 1, "parent": 0, "text": { "name": "Tony Obrien", "Title": "Managing Director" }, "children": [ { "id": 2, "parent": 1, "text": { "name": "Tony Obrien", "Title": "Managing Director" }, "children": [ { "id": 4, "parent": 2, "text": { "name": "Tony Obrien", "Title": "Managing Director" }, "children": [] } ] }, { "id": 3, "parent": 1, "text": { "name": "Tony Obrien", "Title": "Managing Director" }, "children": [ { "id": 5, "parent": 3, "text": { "name": "Tony Obrien", "Title": "Managing Director" }, "children": [] } ] }, { "id": 6, "parent": 1, "text": { "name": "Tony Obrien", "Title": "Managing Director" }, "children": [] }, { "id": 7, "parent": 1, "text": { "name": "Tony Obrien", "Title": "Managing Director" }, "children": [] } ] }

在我构建了将其转换为treant.js喜欢的对象的字符串后,我遇到的困难是什么。

例如

var chart_config = {chart: {container: "#collapsable-example",animateOnInit: true,node: {collapsable: true},animation: {nodeAnimation: "easeOutBounce",nodeSpeed: 700,connectorsAnimation: "bounce",connectorsSpeed: 700}},nodeStructure: { "id": 1, "parent": 0, "text": { "name": "Tony Obrien", "Title": "Managing Director" }, "children": [ { "id": 2, "parent": 1, "text": { "name": "Tony Obrien", "Title": "Managing Director" }, "children": [ { "id": 4, "parent": 2, "text": { "name": "Tony Obrien", "Title": "Managing Director" }, "children": [] } ] }, { "id": 3, "parent": 1, "text": { "name": "Tony Obrien", "Title": "Managing Director" }, "children": [ { "id": 5, "parent": 3, "text": { "name": "Tony Obrien", "Title": "Managing Director" }, "children": [] } ] }, { "id": 6, "parent": 1, "text": { "name": "Tony Obrien", "Title": "Managing Director" }, "children": [] }, { "id": 7, "parent": 1, "text": { "name": "Tony Obrien", "Title": "Managing Director" }, "children": [] } ] }

从生成的代码的结果中复制并粘贴JSON时,绝对可以正常工作,但是.....

var tree =  eval({chart: {container: "#collapsable-example",animateOnInit: true,node: {collapsable: true},animation: {nodeAnimation: "easeOutBounce",nodeSpeed: 700,connectorsAnimation: "bounce",connectorsSpeed: 700}},nodeStructure: { "id": 1, "parent": 0, "text": { "name": "Tony Obrien", "Title": "Managing Director" }, "children": [ { "id": 2, "parent": 1, "text": { "name": "Tony Obrien", "Title": "Managing Director" }, "children": [ { "id": 4, "parent": 2, "text": { "name": "Tony Obrien", "Title": "Managing Director" }, "children": [] } ] }, { "id": 3, "parent": 1, "text": { "name": "Tony Obrien", "Title": "Managing Director" }, "children": [ { "id": 5, "parent": 3, "text": { "name": "Tony Obrien", "Title": "Managing Director" }, "children": [] } ] }, { "id": 6, "parent": 1, "text": { "name": "Tony Obrien", "Title": "Managing Director" }, "children": [] }, { "id": 7, "parent": 1, "text": { "name": "Tony Obrien", "Title": "Managing Director" }, "children": [] } ] })

chart_config = tree

这不起作用。我收到意外的令牌错误。我尝试了JSON.parse也无济于事。有人有什么想法吗?

1 个答案:

答案 0 :(得分:1)

您的意外令牌错误很可能来自eval调用。请注意,它takes a string representing js code as an argument。您正在为其提供一个js对象,因此第一个“ {”可能是“意外令牌”。尝试移除eval包装器,我敢打赌它会起作用。