使树形式[{“id": 1},{“id": 2,“children":[{“id":
make a tree form [{"id":1},{"id":2,"children":[{"id":3},{"id":4},{"id":5,"children":[{"id":6}]}]
我在javascript中有一个像这样的字符串变量:
var tree='[{"id":1},{"id":2,"children":[{"id":3},{"id":4},{"id":5,"children"[{"id":6}]}]';
now i want to create a tree from this in which
# 1 and 2 will at same level
# 3 ,4 ,5 will be the sub nodes of 2.
# 6 will be the sub node of 5.
请帮助用javascript或jquery从这个树变量中生成一个树。变量。
在我看来,这没什么大不了的。我发现了一些错别字,但除此之外,它是一个JSON结构。第二个"children"后面少了一个冒号,也少了一些右括号。我去掉了引号
var tree = [{
"id" : 1
}, {
"id" : 2,
"children" : [{
"id" : 3
}, {
"id" : 4
}, {
"id" : 5,
"children" : [{ //missing colon
"id" : 6
} //missing bracket
] //missing bracket
}
]
}
];
console.log(JSON.stringify(tree[0]));
console.log(JSON.stringify(tree[1]));
console.log(JSON.stringify(tree[1].children));
var tree='[{"id":1},{"id":2,"children":[{"id":3},{"id":4},{"id":5,"children"[{"id":6}]}]';
var objTree = JSON.parse(tree);
console.log(objTree);
只是提醒一下,您有几个拼写错误-要找到它们,只需将字符串放入http://jsonlint.com
有一些错别字,请参阅注释。不管怎样,你只需要使用JSON.parse
var tree='[{"id":1},{"id":2,"children":[{"id":3},{"id":4},{"id":5,"children":[{"id":6}]}]}]';
//Missing ':' near 'children' and '}]' at the end
var array = JSON.parse(tree);
相关文章:
- 如何将HTML id分配给元素,以及如何将JavaScript应用于元素
- difference between '$(<%= DDL.ID %>) & $('
- 使用Javascript获取所选选项ID
- 根据id将json数组组合为一个json数组
- 是否有一个JS/jQuery函数可以获取某个类的每个元素的ID
- 铬:“;未捕获的语法错误:意外的标记:"
- 对id以某个字符串开头的元素进行计数
- Javascript复选框函数:;缺少:在属性id之后"
- 取出图像标签alt:丢失"没有alt-id的图像标签是优选的并且不显示丢失的“;
- "无法读取属性'id'未定义的“;解析工厂中声明的数组时
- 无法调用方法“”;getEditResponseUrl"当使用表单ID打开表单时,绑定到工作表的Google
- Jade:带双“的数据id""&”;
- Knockout.js动态选择模板错误:"找不到ID为'的模板'&”;
- 如何获得类似id的"invokedOn”;使用Bootstrap ContextMenu通过右键单击事件显示文
- 可以't$.parseJSON()当字符串保持src=“”时"或者id=“0”"属性
- chrome.identity.getAuthToken返回"错误的客户端id:{0}”;错误
- 使树形式[{“id": 1},{“id": 2,“children":[{“id":
- Javascript-JQuery:找不到id和.html(")或getElementById并设置inner
- "ctl00_ Body”;在asp.net中生成的对象'id
- & lt;跨度id =“display_XXXXX"祝辞& lt; / span>没有显示任何东西,或者显示“未