树形结构的json格式封装

话不多说,直接show me the code ,no bb

数据库表结构如下:

Organization表:

Id 主键

Name 名称

ParentId 父Id

数据库的数据如下:
树形结构的json格式封装_第1张图片

很明显,这是一个多层级的结构,我们需要把他组装成一个树形结构,返回给前端:

定义一个treeModel类:

    public class treemodel
    {
        public organization parent { get; set; }

        public List childrenList { get; set; } = new List();
    }

写一个获取子结构体的方法:GetChildrentList

        private List GetChildrentList(int parentId)
        {
            var query = _mydbContext.organizations.Where(e => e.ParentId == parentId).ToList();
            //return query.ToList().Concat(query.ToList().SelectMany(t => GetChildrentList(t.Id))).ToList();
            List treemodels = new List();
            query.ForEach(or =>
            {
                var treemodelt = new treemodel();
                treemodelt.parent = _mydbContext.organizations.Find(or.Id);
                treemodelt.childrenList.AddRange(GetChildrentList(or.Id));
                treemodels.Add(treemodelt);                
            });
            return treemodels;
        }

方法里面用递归调用获取子结构,能够得到他的最底一级

然后在主方法里面调用这个方法:

            var lst = GetChildrentList(0);

得到的json格式如下:

[{
	"parent": {
		"id": 1,
		"name": "总部",
		"parentId": 0
	},
	"childrenList": [{
		"parent": {
			"id": 2,
			"name": "罗湖区",
			"parentId": 1
		},
		"childrenList": [{
			"parent": {
				"id": 4,
				"name": "罗湖教学部",
				"parentId": 2
			},
			"childrenList": []
		}, {
			"parent": {
				"id": 5,
				"name": "罗湖销售部",
				"parentId": 2
			},
			"childrenList": []
		}, {
			"parent": {
				"id": 6,
				"name": "罗湖市场部",
				"parentId": 2
			},
			"childrenList": []
		}, {
			"parent": {
				"id": 7,
				"name": "罗外",
				"parentId": 2
			},
			"childrenList": [{
				"parent": {
					"id": 8,
					"name": "教学部",
					"parentId": 7
				},
				"childrenList": []
			}, {
				"parent": {
					"id": 9,
					"name": "教务部",
					"parentId": 7
				},
				"childrenList": []
			}, {
				"parent": {
					"id": 10,
					"name": "行政部",
					"parentId": 7
				},
				"childrenList": []
			}, {
				"parent": {
					"id": 11,
					"name": "销售部",
					"parentId": 7
				},
				"childrenList": []
			}, {
				"parent": {
					"id": 12,
					"name": "市场部",
					"parentId": 7
				},
				"childrenList": []
			}]
		}]
	}, {
		"parent": {
			"id": 3,
			"name": "福田区",
			"parentId": 1
		},
		"childrenList": [{
			"parent": {
				"id": 13,
				"name": "福田实验学校",
				"parentId": 3
			},
			"childrenList": [{
				"parent": {
					"id": 14,
					"name": "教学部",
					"parentId": 13
				},
				"childrenList": []
			}, {
				"parent": {
					"id": 15,
					"name": "教务部",
					"parentId": 13
				},
				"childrenList": []
			}, {
				"parent": {
					"id": 16,
					"name": "新政部",
					"parentId": 13
				},
				"childrenList": []
			}, {
				"parent": {
					"id": 17,
					"name": "销售部",
					"parentId": 13
				},
				"childrenList": []
			}, {
				"parent": {
					"id": 18,
					"name": "市场部",
					"parentId": 13
				},
				"childrenList": []
			}]
		}]
	}]
}]

 

你可能感兴趣的:(数据库设计)