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();
        }
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM