zoukankan      html  css  js  c++  java
  • c# List列表数据转换成树形结构

    把List列表结构 转换成树形结构
     /// <summary>
        /// 构造树形Json
        /// </summary>
        public static class TreeJson
        {
    
            /// <summary>
            /// 转换树Json
            /// </summary>
            /// <param name="list">数据源</param>
            /// <param name="parentId">父节点</param>
            /// <returns></returns>
            public static string ZTreeJson(this List<TreeEntity> list, string parentId = null)   //GetDepartmentTree等方法会给parentId赋值的是null,所以这里的parentId不能写""
            {
                StringBuilder strJson = new StringBuilder();
                List<TreeEntity> item = list.FindAll(t => t.parentId == parentId);
                strJson.Append("[");
                if (item.Count > 0)
                {
                    foreach (TreeEntity entity in item)
                    {
                        strJson.Append("{");
                        strJson.Append("\"id\":\"" + entity.id + "\",");
                        if (!string.IsNullOrEmpty(entity.text))
                        {
                            strJson.Append("\"name\":\"" + entity.text.Replace("&nbsp;", "") + "\",");
                        }
                        if (!string.IsNullOrEmpty(entity.Attribute))
                        {
                            strJson.Append("\"" + entity.Attribute + "\":\"" + entity.AttributeValue + "\",");
                        }
                        if (!string.IsNullOrEmpty(entity.AttributeA))
                        {
                            strJson.Append("\"" + entity.AttributeA + "\":\"" + entity.AttributeValueA + "\",");
                        }
                        if (!string.IsNullOrEmpty(entity.AttributeB))
                        {
                            strJson.Append("\"" + entity.AttributeB + "\":\"" + entity.AttributeValueB + "\",");
                        }
                        if (!string.IsNullOrEmpty(entity.AttributeC))
                        {
                            strJson.Append("\"" + entity.AttributeC + "\":\"" + entity.AttributeValueC + "\",");
                        }
                        if (entity.checkstate != null)
                        {
                            strJson.Append("\"checked\":" + entity.checkstate + ",");
                        }
                        strJson.Append("\"showcheck\":" + entity.showcheck.ToString().ToLower() + ",");
                        strJson.Append("\"open\":" + entity.isexpand.ToString().ToLower() + ",");
                        strJson.Append("\"isParent\":" + entity.hasChildren.ToString().ToLower() + ",");
                        strJson.Append("\"children\":" + ZTreeJson(list, entity.id) + "");
                        strJson.Append("},");
                    }
                    strJson = strJson.Remove(strJson.Length - 1, 1);
                }
                strJson.Append("]");
                return strJson.ToString();
            }
    
    
    
            /// <summary>
            /// 转换树Json
            /// </summary>
            /// <param name="list">数据源</param>
            /// <param name="ParentId">父节点</param>
            /// <returns></returns>
            public static string TreeToJson(this List<TreeEntity> list, string ParentId = null)   //GetDepartmentTree等方法会给parentId赋值的是null,所以这里的parentId不能写""
            {
                StringBuilder strJson = new StringBuilder();
                List<TreeEntity> item = list.FindAll(t => t.parentId == ParentId);
                strJson.Append("[");
                if (item.Count > 0)
                {
                    foreach (TreeEntity entity in item)
                    {
                        strJson.Append("{");
                        strJson.Append("\"id\":\"" + entity.id + "\",");
                        if (!string.IsNullOrEmpty(entity.text))
                        {
                            strJson.Append("\"text\":\"" + entity.text.Replace("&nbsp;", "") + "\",");
                        }
                        if (!string.IsNullOrEmpty(entity.value))
                        {
                            strJson.Append("\"value\":\"" + entity.value + "\",");
                        }
    
                        if (!string.IsNullOrEmpty(entity.Attribute))
                        {
                            strJson.Append("\"" + entity.Attribute + "\":\"" + entity.AttributeValue + "\",");
                        }
                        if (!string.IsNullOrEmpty(entity.AttributeA))
                        {
                            strJson.Append("\"" + entity.AttributeA + "\":\"" + entity.AttributeValueA + "\",");
                        }
                        if (entity.title != null && !string.IsNullOrEmpty(entity.title.Replace("&nbsp;", "")))
                        {
                            strJson.Append("\"title\":\"" + entity.title.Replace("&nbsp;", "") + "\",");
                        }
                        if (entity.img != null && !string.IsNullOrEmpty(entity.img.Replace("&nbsp;", "")))
                        {
                            strJson.Append("\"img\":\"" + entity.img.Replace("&nbsp;", "") + "\",");
                        }
                        if (entity.checkstate != null)
                        {
                            strJson.Append("\"checkstate\":" + entity.checkstate + ",");
                        }
                        //if (entity.parentId != null)
                        //{
                        //    strJson.Append("\"parentnodes\":\"" + entity.parentId + "\",");
                        //}
                        strJson.Append("\"parentnodes\":\"" + entity.parentId + "\",");
                        if (entity.Level != null)
                        {
                            strJson.Append("\"Level\":" + entity.Level + ",");
                        }
                        strJson.Append("\"showcheck\":" + entity.showcheck.ToString().ToLower() + ",");
                        strJson.Append("\"isexpand\":" + entity.isexpand.ToString().ToLower() + ",");
                        if (entity.complete == true)
                        {
                            strJson.Append("\"complete\":" + entity.complete.ToString().ToLower() + ",");
                        }
                        strJson.Append("\"hasChildren\":" + entity.hasChildren.ToString().ToLower() + ",");
                        strJson.Append("\"ChildNodes\":" + TreeToJson(list, entity.id) + "");
                        strJson.Append("},");
                    }
                    strJson = strJson.Remove(strJson.Length - 1, 1);
                }
                strJson.Append("]");
                return strJson.ToString();
            }
        }
  • 相关阅读:
    IO模式和IO多路复用详解
    消息队列RabbitMQ、缓存数据库Redis
    rest framework认证组件和django自带csrf组件区别详解
    django进阶之缓存
    关于CSRF攻击详解
    Linux学习常用命令大全
    .NET 开源工作流: Slickflow流程引擎基础介绍(四) -- 多数据库支持实现
    .NET 开源工作流: Slickflow流程引擎基础介绍(三) -- 基于HTML5/Bootstrap的Web流程设计器
    .NET 开源工作流: Slickflow流程引擎基础介绍(二) -- 引擎组件和业务系统的集成
    .NET开源敏捷开发框架: SlickOne介绍(一) -- 基于Dapper, Mvc和WebAPI 的快速开发框架
  • 原文地址:https://www.cnblogs.com/zhaops/p/10677718.html
Copyright © 2011-2022 走看看