后台拼接tree的json格式
public static string GetChannelTreeNodeJson() { Database db = DatabaseFactory.CreateDatabase(); string sql = @"SELECT PageId ,pageName ,cregex ,parentId ,dregex FROM LogFile_Page "; DbCommand mycmd = db.GetSqlStringCommand(sql); DataSet ds = db.ExecuteDataSet(mycmd); System.Text.StringBuilder s = new System.Text.StringBuilder(); if (ds != null && ds.Tables.Count > 0) { DataTable dt = ds.Tables[0]; s.Append(GetChildNodes(dt, "00")); s = s.Remove(s.Length - 2, 2); } return s.ToString(); } public static string GetChildNodes(DataTable dt, string parentid) { StringBuilder stringbuilder = new StringBuilder(); DataRow[] CRow = dt.Select("parentId=" + parentid); if (CRow.Length > 0) { stringbuilder.Append("["); for (int i = 0; i < CRow.Length; i++) { string chidstring = GetChildNodes(dt, CRow[i]["PageId"].ToString()); if (!string.IsNullOrEmpty(chidstring)) { stringbuilder.Append("{ \"id\":\"" + CRow[i]["PageId"].ToString() + "\",\"text\":\"" + CRow[i]["pageName"].ToString() + "\",\"state\":\"closed\",\"children\":"); stringbuilder.Append(chidstring); } else { stringbuilder.Append("{\"id\":\"" + CRow[i]["PageId"].ToString() + "\",\"text\":\"" + CRow[i]["pageName"].ToString() + "\"},"); } } stringbuilder.Replace(',', ' ', stringbuilder.Length - 1, 1); stringbuilder.Append("]},"); } return stringbuilder.ToString(); }