• 生成树形结构的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": []
                }
            ]
        }
    ]
  • 相关阅读:
    宫立秋20201015-3 每周例行报告
    宫立秋 20201008-1 每周例行报告
    分析“中国铁路12306”查询某个火车经过的车站和预计到达时间的request请求和respond响应。
    来自童晶老师的游戏开发课程作业
    飞机大战
    总结
    20201207-总结
    实时时钟
    作业要求 20201126-1 每周例行报告
    要求 20201120-1 每周例行报告
  • 原文地址:https://www.cnblogs.com/cby-love/p/5513066.html
Copyright © 2020-2023  润新知