生成树形结构的JSON字符串代码(C#)供前端Angular tree使用.
框架是使用EF6.0.可以针对返回的值使用Newtonsoft.Json.dll(百度搜一下)来对返回的值序列化为json字符串,如果对以下值那就是使用JsonConvert.SerializeObject(functionTree),啥都不说,上实例代码
/// <summary> /// init tree /// </summary> /// <returns></returns> public List<FunctionInfoMapping> LoadTree() { List<FunctionInfoMapping> listTree = InitTree(); return listTree; //return JsonConvert.SerializeObject(list); } /// <summary> /// init tree find top menu /// </summary> /// <returns></returns> private List<FunctionInfoMapping> InitTree() { RightsContext rightContext = new RightsContext(); var treeList = (from a in db.FunctionInfoes join b in db.FunctionInfoes on a.ParentId equals b.FunctionId select new FunctionInfoMapping { ID=a.FunctionId, Title=a.FunctionName, FunctionType=a.FunctionType, ParentId=b.FunctionId, ParentName=b.FunctionName, FunctionPath=a.FunctionPath, Description=a.Description, SortId=a.SortId, }).Union (from a in db.FunctionInfoes where a.ParentId == -1 select new FunctionInfoMapping { ID = a.FunctionId, Title = a.FunctionName, FunctionType = a.FunctionType, ParentId = -1, ParentName = "", FunctionPath = a.FunctionPath, Description = a.Description, SortId = a.SortId, }); var newTree = treeList.Union(treeList); //List<FunctionInfoMapping> reeList= treeList.ToList<FunctionInfoMapping>() List < FunctionInfoMapping > rootNode = new List<FunctionInfoMapping>(); foreach (var plist in newTree.Where(t => t.ParentId == -1)) { FunctionInfoMapping node = new FunctionInfoMapping(); node.ID = plist.ID; node.Title = plist.Title; node.FunctionType = plist.FunctionType; node.ParentId = plist.ParentId; node.ParentName = plist.ParentName; node.FunctionPath = plist.FunctionPath; node.Description = plist.Description; node.SortId =plist.SortId; node.Nodes = CreateChildTree(newTree.AsQueryable<FunctionInfoMapping>(), node); rootNode.Add(node); } return rootNode; } /// <summary> /// recursive /// </summary> /// <param name="TreeList"></param> /// <param name="jt"></param> /// <returns></returns> private List<FunctionInfoMapping> CreateChildTree(IQueryable<FunctionInfoMapping> TreeList, FunctionInfoMapping parentId) { int keyid = parentId.ID;//root id List<FunctionInfoMapping> nodeList = new List<FunctionInfoMapping>(); var children = TreeList.Where(t => t.ParentId == keyid); foreach (var chl in children) { FunctionInfoMapping node = new FunctionInfoMapping(); node.ID = chl.ID; node.Title = chl.Title; node.FunctionType = chl.FunctionType; node.ParentId = chl.ParentId; node.ParentName = chl.ParentName; node.FunctionPath = chl.FunctionPath; node.Description = chl.Description; node.SortId = chl.SortId; node.Nodes = CreateChildTree(TreeList, node); nodeList.Add(node); } return nodeList; }
返回的结构如下
[
{
"id": 2,
"title": "Fundamental",
"functiontype": 1,
"parentId": -1,
"parentname": "",
"functionpath": "/Html/Fundamental",
"description": "fundamental menu link",
"sortid": 0,
"nodes": []
},
{
"id": 3,
"title": "Auth Manager",
"functiontype": 1,
"parentId": -1,
"parentname": "",
"functionpath": "/Html/Auth",
"description": " auth manager link ",
"sortid": 0,
"nodes": [
{
"id": 4,
"title": "Role Manager",
"functiontype": 2,
"parentId": 3,
"parentname": "Auth Manager",
"functionpath": "/Html/Auth/roles.html",
"description": " roles manager page ",
"sortid": 0,
"nodes": [
{
"id": 10,
"title": "Add Role",
"functiontype": 3,
"parentId": 4,
"parentname": "Role Manager",
"functionpath": null,
"description": null,
"sortid": 0,
"nodes": []
},
{
"id": 12,
"title": "Delete role",
"functiontype": 3,
"parentId": 4,
"parentname": "Role Manager",
"functionpath": null,
"description": null,
"sortid": 0,
"nodes": []
}
]
},
{
"id": 5,
"title": "Page Manager",
"functiontype": 2,
"parentId": 3,
"parentname": "Auth Manager",
"functionpath": "/Html/Auth/pages.html",
"description": "pages permission manager page",
"sortid": 0,
"nodes": []
}
]
}
]
作者:tuohaibei
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须在文章页面给出原文连接,否则保留追究法律责任的权利.
如果您觉得文章对您有帮助,可以点击文章右下角"推荐".您的鼓励是作者坚持原创和持续写作的最大动力!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?