日期:2014-05-16  浏览次数:20618 次

asp.net数据库生成LigerUi树Tree结构Json格式数据(有点绕,附基本源代码)
        // ----------------------------------- // 取Select数据

        string Sql = "Select Class_Id As Id,Class_Name as text,Father_Id as fatherid,Tree_Sub,Tree_End,Id as children from Sys_Class"
                  + " Where Deleted<>1 or Deleted is Null order by Tree_Sort";

        Sql_Class SqlClass = new Sql_Class();

        SqlDataReader dr = SqlClass.Sql_Select(Sql);

        StringBuilder jsonString = new StringBuilder();

        //jsonString.Append("{");   // 总括号 -- 开始
        //jsonString.Append('"' + "Total" + '"' + ":" + '"' + Count_1.ToString() + '"' + ",");   // 字段总数
        //jsonString.Append('"' + "Rows" + '"' + ":");   // 行字段开始

        jsonString.Append("[");   // 字段开始
        int s = 0;
        string m_Sub = "";  // 是否有子类
        string m_End = "";   // 父类Id

        while (dr.Read())  // ------------- 记录循环
        {
            s = s + 1;
            jsonString.Append("{");
            m_Sub = "";
            m_End = "";
            for (int i = 0; i < dr.FieldCount; i++)  // ---------- 字段循环
            {
                Type type = dr.GetFieldType(i);
                string strKey = dr.GetName(i);    // 字段名
                string strValue = dr[i].ToString(); // 字段内容

                ///LogManager.WriteLog("第"+s.ToString()+"行:   dr.GetName(i)的字段名:" + strKey + "    dr[i]字段内容" + strValue);

                // ------------------------------------ // 检查赋值【有无子记录】和【是否子记录结束】

                if (strKey == "Tree_Sub" || strKey == "Tree_End")
                {

                    if (strKey == "Tree_Sub")
                    {
                        m_Sub = strValue;

                    }
                    else
                    {
                        m_End = strValue;
                        // LogManager.WriteLog("m_End:" + m_End);
                    }

                }
                else
                {


                    // ------------------------------------ // 取字段并赋值

                    jsonString.Append("\"" + strKey + "\":");  // ------ // 赋值 -- 字段名

                    if (strKey != "children")
                    {

                         //StrFormat sss=new StrFormat();
                        strValue = StrFormat.StringFormat(strValue, type); // ------ 赋值 -- 字段内容
                        if (i < dr.FieldCount - 1)
                        {
                            jsonString.Append(strValue + ",");
                        }
                        else
                        {
                            jsonString.Append(strValue);
                        }

                        // --------------- // 如果


                    }
                    else
                    {

                        //LogManager.WriteLog("第" + s.ToString() + "行:   dr.GetName(i)的字段名:" + strKey + "    dr[i]字段内容" + strValue + "m_Sub:" + m_Sub);
                        // -------------------------------------------- // 如果有子树

                        if (m_Sub == "1")
                        {
                            // LogManager.WriteLog("m_Sub:" + m_Sub);
                            jsonString.Append("[");
                        }
                        else
                        {
                            jsonString.Append("null");
                        }
                    }

                }

            }  // End_For

            // -------------------------------------------- // 计算子树类

            if (m_Sub != "1" && m_End != "1")
            {
                jsonString.Append("},"