python使用json实现树结构_js怎样将获取json转换为树形结构
最近elementUI的級聯選擇器要用到 根據國內外方案 做了一個我需要的!
https://jsfiddle.net/stywell/...
var list = [
{
"ID": 1,
"FatherID": 0,
"TypeName": "風電",
"SortID": 2,
"Depth": 0,
"RootID": 1,
"IsFather": true
},
{
"ID": 2,
"FatherID": 1,
"TypeName": "風力發電",
"SortID": 11,
"Depth": 1,
"RootID": 1,
"IsFather": false
},
{
"ID": 13,
"FatherID": 1,
"TypeName": "風電整機",
"SortID": 13,
"Depth": 1,
"RootID": 1,
"IsFather": false
},
{
"ID": 23,
"FatherID": 1,
"TypeName": "葉片及材料",
"SortID": 14,
"Depth": 1,
"RootID": 1,
"IsFather": false
},
{
"ID": 33,
"FatherID": 1,
"TypeName": "發電機及元件",
"SortID": 15,
"Depth": 1,
"RootID": 1,
"IsFather": false
},
{
"ID": 44,
"FatherID": 1,
"TypeName": "塔筒/塔架",
"SortID": 16,
"Depth": 1,
"RootID": 1,
"IsFather": false
},
{
"ID": 54,
"FatherID": 1,
"TypeName": "控制系統",
"SortID": 18,
"Depth": 1,
"RootID": 1,
"IsFather": false
},
{
"ID": 65,
"FatherID": 0,
"TypeName": "核電",
"SortID": 4,
"Depth": 0,
"RootID": 65,
"IsFather": true
},
{
"ID": 77,
"FatherID": 65,
"TypeName": "核電站",
"SortID": 651,
"Depth": 1,
"RootID": 65,
"IsFather": false
},
{
"ID": 66,
"FatherID": 65,
"TypeName": "核電設備",
"SortID": 652,
"Depth": 1,
"RootID": 65,
"IsFather": false
},
{
"ID": 91,
"FatherID": 65,
"TypeName": "核電工程",
"SortID": 653,
"Depth": 1,
"RootID": 65,
"IsFather": false
},
{
"ID": 103,
"FatherID": 0,
"TypeName": "水電",
"SortID": 3,
"Depth": 0,
"RootID": 103,
"IsFather": true
},
{
"ID": 117,
"FatherID": 103,
"TypeName": "水電站",
"SortID": 1031,
"Depth": 1,
"RootID": 103,
"IsFather": false
},
{
"ID": 104,
"FatherID": 103,
"TypeName": "水電設備",
"SortID": 1032,
"Depth": 1,
"RootID": 103,
"IsFather": false
},
{
"ID": 130,
"FatherID": 103,
"TypeName": "水利水電設計院",
"SortID": 1033,
"Depth": 1,
"RootID": 103,
"IsFather": false
},
{
"ID": 136,
"FatherID": 103,
"TypeName": "水電工程",
"SortID": 1034,
"Depth": 1,
"RootID": 103,
"IsFather": false
},
{
"ID": 699,
"FatherID": 0,
"TypeName": "環保",
"SortID": 6,
"Depth": 0,
"RootID": 699,
"IsFather": true
},
{
"ID": 700,
"FatherID": 699,
"TypeName": "水處理",
"SortID": 6991,
"Depth": 1,
"RootID": 699,
"IsFather": true
},
{
"ID": 720,
"FatherID": 699,
"TypeName": "脫硫脫硝",
"SortID": 6992,
"Depth": 1,
"RootID": 699,
"IsFather": false
},
{
"ID": 740,
"FatherID": 699,
"TypeName": "環保設備",
"SortID": 6993,
"Depth": 1,
"RootID": 699,
"IsFather": false
},
{
"ID": 760,
"FatherID": 699,
"TypeName": "固廢處理",
"SortID": 6994,
"Depth": 1,
"RootID": 699,
"IsFather": false
},
{
"ID": 780,
"FatherID": 699,
"TypeName": "環保發電",
"SortID": 6995,
"Depth": 1,
"RootID": 699,
"IsFather": false
},
{
"ID": 794,
"FatherID": 699,
"TypeName": "環保工程",
"SortID": 6998,
"Depth": 1,
"RootID": 699,
"IsFather": false
},
{
"ID": 811,
"FatherID": 699,
"TypeName": "除灰除塵",
"SortID": 69910,
"Depth": 1,
"RootID": 699,
"IsFather": false
},
{
"ID": 853,
"FatherID": 699,
"TypeName": "環境監測與評價",
"SortID": 69912,
"Depth": 1,
"RootID": 699,
"IsFather": false
},
{
"ID": 864,
"FatherID": 699,
"TypeName": "建筑節能",
"SortID": 69913,
"Depth": 1,
"RootID": 699,
"IsFather": false
},
{
"ID": 874,
"FatherID": 699,
"TypeName": "工業節能",
"SortID": 69914,
"Depth": 1,
"RootID": 699,
"IsFather": false
},
{
"ID": 881,
"FatherID": 699,
"TypeName": "合同能源管理",
"SortID": 69915,
"Depth": 1,
"RootID": 699,
"IsFather": false
},
{
"ID": 895,
"FatherID": 699,
"TypeName": "節能設備",
"SortID": 69916,
"Depth": 1,
"RootID": 699,
"IsFather": false
}
];
var list2 = [
{
"ID": 1,
"FatherID": -1,
"TypeName": "風電",
"SortID": 2,
"Depth": 0,
"RootID": 1,
"IsFather": true
},
{
"ID": 2,
"FatherID": 1,
"TypeName": "風力發電",
"SortID": 11,
"Depth": 1,
"RootID": 1,
"IsFather": false
},
{
"ID": 13,
"FatherID": 1,
"TypeName": "風電整機",
"SortID": 13,
"Depth": 1,
"RootID": 1,
"IsFather": false
},
{
"ID": 23,
"FatherID": 1,
"TypeName": "葉片及材料",
"SortID": 14,
"Depth": 1,
"RootID": 1,
"IsFather": false
},
{
"ID": 33,
"FatherID": 1,
"TypeName": "發電機及元件",
"SortID": 15,
"Depth": 1,
"RootID": 1,
"IsFather": false
},
{
"ID": 44,
"FatherID": 1,
"TypeName": "塔筒/塔架",
"SortID": 16,
"Depth": 1,
"RootID": 1,
"IsFather": false
},
{
"ID": 54,
"FatherID": 1,
"TypeName": "控制系統",
"SortID": 18,
"Depth": 1,
"RootID": 1,
"IsFather": false
},
{
"ID": 65,
"FatherID": -1,
"TypeName": "核電",
"SortID": 4,
"Depth": 0,
"RootID": 65,
"IsFather": true
},
{
"ID": 77,
"FatherID": 65,
"TypeName": "核電站",
"SortID": 651,
"Depth": 1,
"RootID": 65,
"IsFather": false
},
{
"ID": 66,
"FatherID": 65,
"TypeName": "核電設備",
"SortID": 652,
"Depth": 1,
"RootID": 65,
"IsFather": false
},
{
"ID": 91,
"FatherID": 65,
"TypeName": "核電工程",
"SortID": 653,
"Depth": 1,
"RootID": 65,
"IsFather": false
},
{
"ID": 103,
"FatherID": -1,
"TypeName": "水電",
"SortID": 3,
"Depth": 0,
"RootID": 103,
"IsFather": true
},
{
"ID": 117,
"FatherID": 103,
"TypeName": "水電站",
"SortID": 1031,
"Depth": 1,
"RootID": 103,
"IsFather": false
},
{
"ID": 104,
"FatherID": 103,
"TypeName": "水電設備",
"SortID": 1032,
"Depth": 1,
"RootID": 103,
"IsFather": false
},
{
"ID": 130,
"FatherID": 103,
"TypeName": "水利水電設計院",
"SortID": 1033,
"Depth": 1,
"RootID": 103,
"IsFather": false
},
{
"ID": 136,
"FatherID": 103,
"TypeName": "水電工程",
"SortID": 1034,
"Depth": 1,
"RootID": 103,
"IsFather": false
},
{
"ID": 699,
"FatherID": -1,
"TypeName": "環保",
"SortID": 6,
"Depth": 0,
"RootID": 699,
"IsFather": true
},
{
"ID": 700,
"FatherID": 699,
"TypeName": "水處理",
"SortID": 6991,
"Depth": 1,
"RootID": 699,
"IsFather": true
},
{
"ID": 720,
"FatherID": 699,
"TypeName": "脫硫脫硝",
"SortID": 6992,
"Depth": 1,
"RootID": 699,
"IsFather": false
},
{
"ID": 740,
"FatherID": 699,
"TypeName": "環保設備",
"SortID": 6993,
"Depth": 1,
"RootID": 699,
"IsFather": false
},
{
"ID": 760,
"FatherID": 699,
"TypeName": "固廢處理",
"SortID": 6994,
"Depth": 1,
"RootID": 699,
"IsFather": false
},
{
"ID": 780,
"FatherID": 699,
"TypeName": "環保發電",
"SortID": 6995,
"Depth": 1,
"RootID": 699,
"IsFather": false
},
{
"ID": 794,
"FatherID": 699,
"TypeName": "環保工程",
"SortID": 6998,
"Depth": 1,
"RootID": 699,
"IsFather": false
},
{
"ID": 811,
"FatherID": 699,
"TypeName": "除灰除塵",
"SortID": 69910,
"Depth": 1,
"RootID": 699,
"IsFather": false
},
{
"ID": 853,
"FatherID": 699,
"TypeName": "環境監測與評價",
"SortID": 69912,
"Depth": 1,
"RootID": 699,
"IsFather": false
},
{
"ID": 864,
"FatherID": 699,
"TypeName": "建筑節能",
"SortID": 69913,
"Depth": 1,
"RootID": 699,
"IsFather": false
},
{
"ID": 874,
"FatherID": 699,
"TypeName": "工業節能",
"SortID": 69914,
"Depth": 1,
"RootID": 699,
"IsFather": false
},
{
"ID": 881,
"FatherID": 699,
"TypeName": "合同能源管理",
"SortID": 69915,
"Depth": 1,
"RootID": 699,
"IsFather": false
},
{
"ID": 895,
"FatherID": 699,
"TypeName": "節能設備",
"SortID": 69916,
"Depth": 1,
"RootID": 699,
"IsFather": false
}
];
var list3 = [
{
"tid": 1,
"parent": -1,
"TypeName": "風電",
"SortID": 2,
},
{
"tid": 2,
"parent": 1,
"TypeName": "風力發電",
"SortID": 11,
},
{
"tid": 13,
"parent": 1,
"TypeName": "風電整機",
"SortID": 13,
},
{
"tid": 23,
"parent": 1,
"TypeName": "葉片及材料",
"SortID": 14,
},
{
"tid": 33,
"parent": 1,
"TypeName": "發電機及元件",
"SortID": 15,
},
{
"tid": 44,
"parent": 1,
"TypeName": "塔筒/塔架",
"SortID": 16,
},
{
"tid": 54,
"parent": 1,
"TypeName": "控制系統",
"SortID": 18,
},
{
"tid": 65,
"parent": -1,
"TypeName": "核電",
"SortID": 4,
},
{
"tid": 77,
"parent": 65,
"TypeName": "核電站",
"SortID": 651,
},
{
"tid": 66,
"parent": 65,
"TypeName": "核電設備",
"SortID": 652,
},
{
"tid": 91,
"parent": 65,
"TypeName": "核電工程",
"SortID": 653,
},
{
"tid": 103,
"parent": -1,
"TypeName": "水電",
"SortID": 3,
},
{
"tid": 117,
"parent": 103,
"TypeName": "水電站",
"SortID": 1031,
},
{
"tid": 104,
"parent": 103,
"TypeName": "水電設備",
"SortID": 1032,
},
{
"tid": 130,
"parent": 103,
"TypeName": "水利水電設計院",
"SortID": 1033,
},
{
"tid": 136,
"parent": 103,
"TypeName": "水電工程",
"SortID": 1034,
},
{
"tid": 699,
"parent": -1,
"TypeName": "環保",
"SortID": 6,
},
{
"tid": 700,
"parent": 699,
"TypeName": "水處理",
"SortID": 6991,
},
{
"tid": 720,
"parent": 699,
"TypeName": "脫硫脫硝",
"SortID": 6992,
},
{
"tid": 740,
"parent": 699,
"TypeName": "環保設備",
"SortID": 6993,
},
{
"tid": 760,
"parent": 699,
"TypeName": "固廢處理",
"SortID": 6994,
},
{
"tid": 780,
"parent": 699,
"TypeName": "環保發電",
"SortID": 6995,
},
{
"tid": 794,
"parent": 699,
"TypeName": "環保工程",
"SortID": 6998,
},
{
"tid": 811,
"parent": 699,
"TypeName": "除灰除塵",
"SortID": 69910,
},
{
"tid": 853,
"parent": 699,
"TypeName": "環境監測與評價",
"SortID": 69912,
},
{
"tid": 864,
"parent": 699,
"TypeName": "建筑節能",
"SortID": 69913,
},
{
"tid": 874,
"parent": 699,
"TypeName": "工業節能",
"SortID": 69914,
},
{
"tid": 881,
"parent": 699,
"TypeName": "合同能源管理",
"SortID": 69915,
},
{
"tid": 895,
"parent": 699,
"TypeName": "節能設備",
"SortID": 69916,
}
];
function list2tree(data, opt) {
opt = opt || {};
var KEY_ID = opt.key_id || 'ID';
var KEY_PARENT = opt.key_parent || 'FatherID';
var KEY_CHILD = opt.key_child || 'children';
var EMPTY_CHILDREN = opt.empty_children;
var ROOT_ID = opt.root_id || 0;
var MAP = opt.map || {};
function getNode(id) {
var node = []
for (var i = 0; i < data.length; i++) {
if (data[i][KEY_PARENT] == id) {
for (var k in MAP) {
data[i][k] = data[i][MAP[k]];
}
if (getNode(data[i][KEY_ID]) !== undefined) {
data[i][KEY_CHILD] = getNode(data[i][KEY_ID]);
} else {
if (EMPTY_CHILDREN === null) {
data[i][KEY_CHILD] = null;
} else if (JSON.stringify(EMPTY_CHILDREN) === '[]') {
data[i][KEY_CHILD] = [];
}
}
node.push(data[i]);
}
}
if (node.length == 0) {
return;
} else {
return node;
}
}
return getNode(ROOT_ID)
}
var opt = {
"key_id": "ID", //節點的ID
"key_parent": "FatherID", //節點的父級ID
"key_child": "children", //子節點的名稱
"empty_children": [], //子節點為空時,填充的值 //這個參數為空時,沒有子元素的元素不帶key_child屬性;還可以為null或者[],同理
"root_id": 0, //根節點的父級ID
"map": { //在節點內映射一些值 //對象的鍵是節點的新屬性; 對象的值是節點的老屬性,會賦值給新屬性
"value": "ID",
"label": "TypeName",
}
}
//默認
// var res = list2tree(list});
//添加映射
// var res = list2tree(list, {
// map: {"value": "ID","label": "TypeName"}
// });
//設置子節點為空時的類型 可以為 [] null 或者 不存在那個屬性
// var res = list2tree(list, {
// map: {"value": "ID","label": "TypeName"},
// empty_children: [],
// });
//根元素ID不為0時的情況
// var res = list2tree(list2, {
// map: {"value": "ID","label": "TypeName"},
// empty_children: [],
// root_id: -1,
// });
//全參數
var res = list2tree(list3, {
map: {
"value": "tid",
"label": "TypeName",
"foo": "tid",
},
//empty_children: null,
root_id: -1,
key_id: 'tid',
key_parent: 'parent',
key_child: 'nodes'
});
console.log(res);
document.write('
');document.write(JSON.stringify(res, null, 2));
總結
以上是生活随笔為你收集整理的python使用json实现树结构_js怎样将获取json转换为树形结构的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 华为M2多少钱
- 下一篇: 美颜相机怎么在电脑用?美颜相机电脑使用方