【发布时间】:2015-07-28 21:25:11
【问题描述】:
我有一个 JSON 字符串:
{ "a1": "root",
"a2": "root data",
"children": [
{ "a1": "child 1",
"a2": "child 1 data",
"children": []
},
{ "a1": "child 2",
"a2": "child 2 data",
"children": [
{ "a1": "child 3",
"a2": "child 3 data",
"children": []
}
]
}
]
}
我想将这个 JSON 树结构的字符串读入一个 JavaScript 对象。我希望 JavaScript 对象的类定义如下:
function MyNode(){
this.a1 = ""
this.a2 = ""
this.children = []
}
基本上在阅读完 JSON 数据结构后,我想要一个 MyNode 类型的实例,它具有参数 a1 a2 和 children,其中 children 或根节点具有 @ 类型的实例987654328@ 和 JSON 字符串中指定的数据/参数。
我怎样才能做到这一点?任何指针都会非常有帮助。
【问题讨论】:
标签: javascript json tree