我怎样才能改变我的json结构?

时间:2015-03-11 18:03:37

标签: javascript json treeview

我有这个json结构:

[
 {
  "Id": 1,
  "Level": 0,
  Expanded: true/false
 },{
  "id": 2,
  "Level": 1,
  "Parent": 1
 },{
  "id": 3,
  "Level": 1,
  "Parent": 1
 }
]

我想这样设置:

[
 {
  "Id": 1,
  "Level": 0,
  "Expanded": true/false, 
  "child":[
    {
     "id": 2,
     "Level": 1,
     "Parent": 1
    },{
     "id": 3,
     "Level": 1,
     "Parent": 1
    }
   ]
 }
]

有什么方法可以轻松完成吗?最终目标是在角度上使用它与bootstrap ui treeview。

1 个答案:

答案 0 :(得分:0)

如果您的json不会超过1个深度级别。

只需

var json_array = json;
var new_json = angular.extend({'child':[json_array[1],json_array[2]]},json_array[0])