文章分类 Classification
JSON数据转换成树型JSON数据结构
稿件来源: 互联网 撰稿作者: 太阳光 发表日期: 2013-06-06 阅读次数: 391 查看权限: 游客查看
JSON数据转换成树型JSON数据结构
<script type="text/javascript"> var json={ "t001p":{"ID":"t001","Name":"node_t001","ParentID":"y001"}, "s001p":{"ID":"s001","Name":"node_t001","ParentID":"y001"}, "m001p":{"ID":"m001","Name":"node_m001","ParentID":"y001"}, "r001p":{"ID":"r001","Name":"node_r001","ParentID":"s001"}, "u001p":{"ID":"u001","Name":"node_u001","ParentID":"s001"}, "h001p":{"ID":"h001","Name":"node_h001","ParentID":"s001"}, "e001p":{"ID":"e001","Name":"node_e001","ParentID":"h001"}, "k001p":{"ID":"k001","Name":"node_k001","ParentID":"h001"}, "j001p":{"ID":"j001","Name":"node_j001","ParentID":"m001"}, "y001p":{"ID":"y001","Name":"node_y001","ParentID":"y001"} }; function convert(source){ var tmp={},parent,n; for(n in source){ var item=source[n]; if(!tmp[item.ID]){ tmp[item.ID]={}; } tmp[item.ID].Name=item.Name; tmp[item.ID].ID=item.ID; if(!tmp[item.ID].children)tmp[item.ID].children=[]; if(item.ID!=item.ParentID){ if(tmp[item.ParentID]){ tmp[item.ParentID].children.push(tmp[item.ID]); }else{ tmp[item.ParentID]={children:[tmp[item.ID]]}; } }else{ parent=item.ID; } } return tmp[parent]; } var obj=convert(json); </script>
关键词: json,树型json 编辑时间: 2013-06-06
0
高兴1
支持0
搞笑0
不解0
谎言0
枪稿0
震惊0
无奈0
无聊0
反对0
愤怒
0%(0)
0%(0)
- 暂无评论
文章图片 article Pictrue
网友评论