鑾峰彇json涓殑灞炴€у€硷紝鐢熸垚鏂癹son

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了鑾峰彇json涓殑灞炴€у€硷紝鐢熸垚鏂癹son相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/ret' title='ret'>ret   cal   鑾峰彇   item   prim   body   bsp   func   UNC   

  1 <!DOCTYPE html>
  2 <html lang="en">
  3 
  4 <head>
  5     <meta charset="UTF-8">
  6     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7     <title>Document</title>
  8 </head>
  9 
 10 <body>
 11     <script>
 12         var originData = [
 13             {
 14                 "DepartmentId": 1,
 15                 "DepartmentName": "鎶€鏈儴",
 16                 "SubDepartments": [],
 17                 "PrimaryKey": 0
 18             },
 19             {
 20                 "DepartmentId": 2,
 21                 "DepartmentName": "閿€鍞腑蹇?,
 22                 "SubDepartments": [
 23                     {
 24                         "SubDepartmentId": 1,
 25                         "DepartmentId": 2,
 26                         "SubDepartmentName": "鏌愭煇绯诲垪1",
 27                         "Teams": [
 28                             {
 29                                 "TeamId": 1,
 30                                 "SubDepartmentId": 1,
 31                                 "DepartmentId": 2,
 32                                 "TeamName": "鏌愭煇鍥㈤槦1",
 33                                 "PrimaryKey": 0
 34                             },
 35                             {
 36                                 "TeamId": 2,
 37                                 "SubDepartmentId": 1,
 38                                 "DepartmentId": 2,
 39                                 "TeamName": "鏌愭煇鍥㈤槦2",
 40                                 "PrimaryKey": 0
 41                             }
 42                         ],
 43                         "PrimaryKey": 0
 44                     },
 45                     {
 46                         "SubDepartmentId": 2,
 47                         "DepartmentId": 2,
 48                         "SubDepartmentName": "鏌愭煇绯诲垪2",
 49                         "Teams": [
 50                             {
 51                                 "TeamId": 3,
 52                                 "SubDepartmentId": 2,
 53                                 "DepartmentId": 2,
 54                                 "TeamName": "鏌愭煇鍥㈤槦3",
 55                                 "PrimaryKey": 0
 56                             }
 57                         ],
 58                         "PrimaryKey": 0
 59                     }
 60                 ],
 61                 "PrimaryKey": 0
 62             }
 63         ];
 64 
 65         function treeConvert(data, idPre, lv) {
 66             var result = [],
 67                 idPre = idPre || 鈥樷€?span style="color: rgba(0, 0, 0, 1)">,
 68                 lv = lv || 1;
 69             // console.log(arguments);
 70             //浼犺繃鏉ョ殑json涓鑾峰彇鍊肩殑灞炴€у悕绉?
 71             var idKeys = [鈥楧epartmentId鈥? 鈥楽ubDepartmentId鈥? 鈥楾eamId鈥?span style="color: rgba(0, 0, 0, 1)">];
 72             var nameKeys = [鈥楧epartmentName鈥? 鈥楽ubDepartmentName鈥? 鈥楾eamName鈥?span style="color: rgba(0, 0, 0, 1)">]
 73             var childrenKeys = [鈥楽ubDepartments鈥? 鈥楾eams鈥? false];
 74 
 75             var curr = lv - 1,
 76                 idKey = idKeys[curr],
 77                 nameKey = nameKeys[curr],
 78                 childrenKey = childrenKeys[curr]
 79 
 80             data.forEach(function (ele, idx) {
 81                 // console.log(ele);
 82                 var item = {
 83                     id: idPre + 鈥榺鈥?+ ele[idKey],
 84                     orgId: ele[idKey],
 85                     name: ele[nameKeys]
 86                 };
 87                 console.log(鈥榠tem鈥?span style="color: rgba(0, 0, 0, 1)">,item);
 88                 if (childrenKey && ele[childrenKey] && ele[childrenKey].length) {
 89                     console.log("go")
 90                     item.children = [];
 91                     item.children.push(treeConvert(ele[childrenKey], item.id, lv + 1));
 92                 }
 93                 console.log("result1",result);
 94                 result.push(item);
 95             });
 96             console.log("result2",result);
 97             return result;            
 98         }
 99         console.log("new_originData",treeConvert(originData));
100 
101     </script>
102 
103 </body>
104 
105 </html>

 

以上是关于鑾峰彇json涓殑灞炴€у€硷紝鐢熸垚鏂癹son的主要内容,如果未能解决你的问题,请参考以下文章

涓汉椤圭洰璇勬祴鈥斺€斾腑灏忓鏁板鍗峰瓙鐢熸垚

鍒嗗竷寮廔D鐢熸垚绛栫暐 · fossi

js 鑾峰彇鎺у埗鍙扮殑閿欒淇℃伅

Tensorflow涓繚瀛樻ā鍨嬫椂鐢熸垚鐨勫悇绉嶆枃浠跺尯鍒拰浣滅敤

鑾峰彇UUID

node js 鑾峰彇绋嬪簭鎵ц鏃堕棿 time timeEnd