生成树形结构的json字符串代码.

Posted 随遇而安

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了生成树形结构的json字符串代码.相关的知识,希望对你有一定的参考价值。

     框架是使用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": []
            }
        ]
    }
]

 

以上是关于生成树形结构的json字符串代码.的主要内容,如果未能解决你的问题,请参考以下文章

JavaScript打印JSON对象 - 树形结构输出 - 格式化JSON数组 - JS从一维数组 生成树形结构对象

JavaScript打印JSON对象 - 树形结构输出 - 格式化JSON数组 - JS从一维数组 生成树形结构对象

JavaScript递归方法 生成 json tree 树形结构数据

JavaScript递归方法 生成 json tree 树形结构数据

JavaScript递归方法 生成 json tree 树形结构数据

JavaScript递归方法 生成 json tree 树形结构数据