如何更改我的 json 结构

How can i change my json structure?

本文关键字:结构 json 我的 何更改      更新时间:2023-09-26

我有这个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
    }
   ]
 }
]

有没有办法轻松做到?最终目标是将其与角度引导 ui 树视图一起使用。

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

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