C# easyui json类
using System;
using System.Data;
using System.Text;
namespace Common
{
public class JsonHelp
{
private StringBuilder result = new StringBuilder();
private StringBuilder sb = new StringBuilder();
public string CreateComboboxJson(DataTable dt, int type)
{
StringBuilder stringBuilder = new StringBuilder();
if (dt != null && dt.Rows.Count > 0)
{
StringBuilder stringBuilder2 = new StringBuilder();
stringBuilder2.Append("[");
if (type != 2)
{
stringBuilder2.Append("{ ");
stringBuilder2.Append("\"id\": ");
stringBuilder2.Append(-1 + ",");
stringBuilder2.Append("\"text\": ");
if (type == 1 || type == 3)
{
stringBuilder2.Append("\"请选择\"");
}
else
{
stringBuilder2.Append("\"全部\"");
}
DataRow[] array = dt.Select("IsDefault=1");
if ((type == 0 || array.Length == 0) && type != 3)
{
stringBuilder2.Append(",\"selected\": ");
stringBuilder2.Append("true");
}
stringBuilder2.Append("},");
}
stringBuilder.Append(stringBuilder2.ToString());
for (int i = 0; i < dt.Rows.Count; i++)
{
if (i < dt.Rows.Count - 1)
{
stringBuilder.Append("{ ");
stringBuilder.Append("\"id\": ");
stringBuilder.Append(dt.Rows[i]["ID"] + ",");
stringBuilder.Append("\"text\": ");
stringBuilder.Append("\"" + this.JsonCharFilter(dt.Rows[i]["name"].ToString()) + "\"");
if (dt.Rows[i]["IsDefault"].ToString() == "1" && type == 1)
{
stringBuilder.Append(",\"selected\": ");
stringBuilder.Append("true");
}
stringBuilder.Append("},");
}
if (i == dt.Rows.Count - 1)
{
stringBuilder.Append("{ ");
stringBuilder.Append("\"id\": ");
stringBuilder.Append(dt.Rows[i]["ID"] + ",");
stringBuilder.Append("\"text\": ");
stringBuilder.Append("\"" + this.JsonCharFilter(dt.Rows[i]["name"].ToString()) + "\"");
if (dt.Rows[i]["IsDefault"].ToString() == "1" && type == 1)
{
stringBuilder.Append(",\"selected\": ");
stringBuilder.Append("true");
}
stringBuilder.Append("}");
}
}
stringBuilder.Append("]");
return stringBuilder.ToString();
}
stringBuilder.Append("[");
stringBuilder.Append("{ ");
stringBuilder.Append("\"id\": ");
stringBuilder.Append(-1 + ",");
stringBuilder.Append("\"text\": ");
stringBuilder.Append("\"请选择\"");
stringBuilder.Append("}");
stringBuilder.Append("]");
return stringBuilder.ToString();
}
public string ComboboxJson(DataTable dt, int warehouseid, int type)
{
StringBuilder stringBuilder = new StringBuilder();
if (dt != null && dt.Rows.Count > 0)
{
StringBuilder stringBuilder2 = new StringBuilder();
stringBuilder2.Append("[");
stringBuilder.Append(stringBuilder2.ToString());
for (int i = 0; i < dt.Rows.Count; i++)
{
if (i < dt.Rows.Count - 1)
{
stringBuilder.Append("{ ");
stringBuilder.Append("\"id\": ");
stringBuilder.Append(dt.Rows[i]["ID"] + ",");
stringBuilder.Append("\"text\": ");
stringBuilder.Append("\"" + this.JsonCharFilter(dt.Rows[i]["name"].ToString()) + "\"");
if ((dt.Rows[i]["name"].ToString() == "人民币" || dt.Rows[i]["ID"].ToString() == "3") && warehouseid == 1 && type == 1)
{
stringBuilder.Append(",\"selected\": ");
stringBuilder.Append("true");
}
if ((dt.Rows[i]["name"].ToString() == "港币" || dt.Rows[i]["ID"].ToString() == "2") && warehouseid == 2 && type == 1)
{
stringBuilder.Append(",\"selected\": ");
stringBuilder.Append("true");
}
stringBuilder.Append("},");
}
if (i == dt.Rows.Count - 1)
{
stringBuilder.Append("{ ");
stringBuilder.Append("\"id\": ");
stringBuilder.Append(dt.Rows[i]["ID"] + ",");
stringBuilder.Append("\"text\": ");
stringBuilder.Append("\"" + this.JsonCharFilter(dt.Rows[i]["name"].ToString()) + "\"");
if ((dt.Rows[i]["name"].ToString() == "人民币" || dt.Rows[i]["ID"].ToString() == "3") && warehouseid == 1 && type == 1)
{
stringBuilder.Append(",\"selected\": ");
stringBuilder.Append("true");
}
if ((dt.Rows[i]["name"].ToString() == "港币" || dt.Rows[i]["ID"].ToString() == "2") && warehouseid == 2 && type == 1)
{
stringBuilder.Append(",\"selected\": ");
stringBuilder.Append("true");
}
stringBuilder.Append("}");
}
}
stringBuilder.Append("]");
return stringBuilder.ToString();
}
stringBuilder.Append("[");
stringBuilder.Append("{ ");
stringBuilder.Append("\"id\": ");
stringBuilder.Append(-1 + ",");
stringBuilder.Append("\"text\": ");
stringBuilder.Append("\"请选择\"");
stringBuilder.Append("}");
stringBuilder.Append("]");
return stringBuilder.ToString();
}
public string CreateComboboxJson(DataTable dt)
{
StringBuilder stringBuilder = new StringBuilder();
if (dt != null && dt.Rows.Count > 0)
{
stringBuilder.Append("[ ");
for (int i = 0; i < dt.Rows.Count; i++)
{
if (i < dt.Rows.Count - 1)
{
stringBuilder.Append("{ ");
stringBuilder.Append("\"id\": ");
stringBuilder.Append(dt.Rows[i]["ID"] + ",");
stringBuilder.Append("\"name\": ");
stringBuilder.Append("\"" + this.JsonCharFilter(dt.Rows[i]["name"].ToString()) + "\"");
stringBuilder.Append("},");
}
if (i == dt.Rows.Count - 1)
{
stringBuilder.Append("{ ");
stringBuilder.Append("\"id\": ");
stringBuilder.Append(dt.Rows[i]["ID"] + ",");
stringBuilder.Append("\"name\": ");
stringBuilder.Append("\"" + this.JsonCharFilter(dt.Rows[i]["name"].ToString()) + "\"");
stringBuilder.Append("}");
}
}
stringBuilder.Append("]");
return stringBuilder.ToString();
}
stringBuilder.Append("[");
stringBuilder.Append("{ ");
stringBuilder.Append("\"id\": ");
stringBuilder.Append(-1 + ",");
stringBuilder.Append("\"text\": ");
stringBuilder.Append("\"请选择\"");
stringBuilder.Append("}");
stringBuilder.Append("]");
return stringBuilder.ToString();
}
public string GetTreeJsonByTable(DataTable tabel, string idCol, string txtCol, string rela, object pId, int type)
{
if (tabel.Rows.Count > 0)
{
this.sb.Append("[");
if (pId.ToString() == "0" && type == 1)
{
this.sb.Append("{\"id\":\"0\",\"text\":\"根目录\",\"state\":\"open\"");
this.sb.Append("},");
}
string filterExpression = string.Format("{0}='{1}'", rela, pId);
DataRow[] array = tabel.Select(filterExpression);
if (array.Length > 0)
{
DataRow[] array2 = array;
for (int i = 0; i < array2.Length; i++)
{
DataRow dataRow = array2[i];
this.sb.Append(string.Concat(new object[]
{
"{\"id\":\"",
dataRow[idCol],
"\",\"text\":\"",
dataRow[txtCol],
"\",\"state\":\"open\""
}));
if (tabel.Select(string.Format("{0}='{1}'", rela, dataRow[idCol])).Length > 0)
{
this.sb.Append(",\"children\":");
this.GetTreeJsonByTable(tabel, idCol, txtCol, rela, dataRow[idCol], type);
this.result.Append(this.sb.ToString());
}
this.result.Append(this.sb.ToString());
this.sb.Append("},");
}
this.sb = this.sb.Remove(this.sb.Length - 1, 1);
}
this.sb.Append("]");
this.result.Append(this.sb.ToString());
}
return this.sb.ToString();
}
public string GetAdvancedRuleTreeJsonByTable(DataTable tabel, string idCol, string txtCol, string rela, object pId)
{
if (tabel.Rows.Count > 0)
{
this.sb.Append("[");
if (pId.ToString() == "0")
{
this.sb.Append("{\"id\":\"0\",\"text\":\"最高权限\",\"state\":\"open\"");
this.sb.Append("},");
}
string filterExpression = string.Format("{0}='{1}'", rela, pId);
DataRow[] array = tabel.Select(filterExpression);
if (array.Length > 0)
{
DataRow[] array2 = array;
for (int i = 0; i < array2.Length; i++)
{
DataRow dataRow = array2[i];
this.sb.Append(string.Concat(new object[]
{
"{\"id\":\"",
dataRow[idCol],
"\",\"text\":\"",
dataRow[txtCol],
"\",\"state\":\"open\""
}));
if (tabel.Select(string.Format("{0}='{1}'", rela, dataRow[idCol])).Length > 0)
{
this.sb.Append(",\"children\":");
this.GetAdvancedRuleTreeJsonByTable(tabel, idCol, txtCol, rela, dataRow[idCol]);
this.result.Append(this.sb.ToString());
}
this.result.Append(this.sb.ToString());
this.sb.Append("},");
}
this.sb = this.sb.Remove(this.sb.Length - 1, 1);
}
this.sb.Append("]");
this.result.Append(this.sb.ToString());
}
else
{
if (pId.ToString() == "0")
{
this.sb.Append("[");
this.sb.Append("{\"id\":\"0\",\"text\":\"最高权限\",\"state\":\"open\"");
this.sb.Append("}");
this.sb.Append("]");
}
}
return this.sb.ToString();
}
public string GetJsonTreeByTable(DataTable dt, string where)
{
StringBuilder stringBuilder = new StringBuilder();
if (dt.Rows.Count > 0)
{
DataRow[] array = dt.Select(where, " Sequence asc");
stringBuilder.Append("[");
for (int i = 0; i < array.Length; i++)
{
stringBuilder.Append("{ ");
int num = Convert.ToInt32(array[i]["id"].ToString());
string text = array[i]["URL"].ToString();
if (!string.IsNullOrEmpty(text) && text.IndexOf('?') != -1)
{
text = text + "&pagesid=" + num;
}
else
{
if (!string.IsNullOrEmpty(text) && text.IndexOf('?') == -1)
{
text = text + "?&pagesid=" + num;
}
}
stringBuilder.Append("\"attributes\": {");
stringBuilder.Append("\"url\": \"" + text + "\",");
stringBuilder.Append("\"isreloadid\":\"" + array[i]["isreloadid"].ToString() + "\"");
stringBuilder.Append("},");
stringBuilder.Append("\"checked\": false, ");
stringBuilder.Append("\"iconCls\": \"ext-icon-medal_gold_3\" ,");
if (array[i]["fatherid"].ToString() == "0")
{
stringBuilder.Append("\"id\": \"" + array[i]["ID"].ToString() + "\" ,");
}
else
{
stringBuilder.Append("\"id\": \"" + array[i]["ID"].ToString() + "\" ,");
stringBuilder.Append("\"pid\": \"" + array[i]["fatherid"].ToString() + "\" ,");
}
stringBuilder.Append("\"state\": \"open\", ");
stringBuilder.Append("\"text\": \"" + array[i]["Name"].ToString().Replace("\"", "\\\"") + "\" ");
if (i == array.Length - 1)
{
stringBuilder.Append("}");
}
else
{
stringBuilder.Append("},");
}
}
stringBuilder.Append("]");
}
else
{
stringBuilder.Append("[]");
}
return stringBuilder.ToString();
}
public string GetFatherNameByFatherID(DataTable dt, string fatherid)
{
DataRow[] array = dt.Select("id=" + fatherid);
if (array.Length > 0)
{
return array[0]["name"].ToString();
}
return "";
}
public string GetTreeList(DataTable dt)
{
DataRow[] array = dt.Select(" fatherid=0");
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.Append("{\"total\":" + dt.Rows.Count + ",\"rows\":");
stringBuilder.Append("[ ");
if (array.Length > 0)
{
for (int i = 0; i < array.Length; i++)
{
stringBuilder.Append("{ ");
int num = Convert.ToInt32(array[i]["fatherid"].ToString());
int num2 = Convert.ToInt32(array[i]["pagetypeid"].ToString());
int num3 = Convert.ToInt32(array[i]["isreloadid"].ToString());
stringBuilder.Append("\"id\": \"" + array[i]["ID"].ToString() + "\" ,");
stringBuilder.Append("\"name\": \"" + array[i]["Name"].ToString().Replace("\"", "\\\"") + "\", ");
stringBuilder.Append("\"seq\": \"" + array[i]["Sequence"].ToString() + "\", ");
stringBuilder.Append("\"url\": \"" + array[i]["url"].ToString().Replace("\"", "\\\"") + "\" ,");
stringBuilder.Append("\"buttonid\": \"" + array[i]["buttonid"].ToString() + "\" ,");
stringBuilder.Append("\"fatherid\": \"" + num + "\" ,");
stringBuilder.Append("\"iconCls\": \"icon-ok\" ,");
stringBuilder.Append("\"pagetypename\": \"" + array[i]["pagetypename"].ToString().Replace("\"", "\\\"") + "\" ,");
stringBuilder.Append("\"pagetypeid\": \"" + num2 + "\" ,");
if (num3 == 1)
{
stringBuilder.Append("\"isreloadid\": \"否\" ,");
}
else
{
stringBuilder.Append("\"isreloadid\": \"是\" ,");
}
stringBuilder.Append("\"state\": \"closed\" ");
stringBuilder.Append("},");
stringBuilder.Append(this.SetMenu(array[i]["ID"].ToString(), dt));
}
}
stringBuilder.Remove(stringBuilder.Length - 1, 1);
stringBuilder.Append("]}");
return stringBuilder.ToString();
}
public string GetAdvancedTreeList(DataTable dt)
{
DataRow[] array = dt.Select(" fatherid=0");
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.Append("{\"total\":" + dt.Rows.Count + ",\"rows\":");
stringBuilder.Append("[ ");
if (array.Length > 0)
{
for (int i = 0; i < array.Length; i++)
{
stringBuilder.Append("{ ");
int num = Convert.ToInt32(array[i]["fatherid"].ToString());
int num2 = Convert.ToInt32(array[i]["userid"].ToString());
string str = array[i]["name"].ToString();
stringBuilder.Append("\"id\": \"" + array[i]["ID"].ToString() + "\" ,");
stringBuilder.Append("\"fatherid\": \"" + num + "\" ,");
stringBuilder.Append("\"userid\": \"" + num2 + "\" ,");
stringBuilder.Append("\"name\": \"" + str + "\" ,");
stringBuilder.Append("\"departmentname\": \"" + array[i]["departmentname"].ToString() + "\" ,");
stringBuilder.Append("\"state\": \"closed\" ");
stringBuilder.Append("},");
stringBuilder.Append(this.SetAdvancedMenu(array[i]["ID"].ToString(), dt));
}
}
stringBuilder.Remove(stringBuilder.Length - 1, 1);
stringBuilder.Append("]}");
return stringBuilder.ToString();
}
public string SetAdvancedMenu(string parentid, DataTable dt)
{
StringBuilder stringBuilder = new StringBuilder();
DataRow[] array = dt.Select(" fatherid=" + parentid);
if (array.Length > 0)
{
for (int i = 0; i < array.Length; i++)
{
stringBuilder.Append("{ ");
int num = Convert.ToInt32(array[i]["userid"].ToString());
string str = array[i]["name"].ToString();
stringBuilder.Append("\"id\": \"" + array[i]["ID"].ToString() + "\" ,");
stringBuilder.Append(string.Format("\"_parentId\": \"{0}\" ,", parentid));
stringBuilder.Append("\"fatherid\": \"" + parentid + "\" ,");
stringBuilder.Append("\"userid\": \"" + num + "\" ,");
stringBuilder.Append("\"name\": \"" + str + "\" ,");
stringBuilder.Append("\"departmentname\": \"" + array[i]["departmentname"].ToString() + "\" ,");
stringBuilder.Append("\"state\": \"closed\" ");
stringBuilder.Append("},");
stringBuilder.Append(this.SetAdvancedMenu(array[i]["ID"].ToString(), dt));
}
}
return stringBuilder.ToString();
}
public string SetMenu(string parentid, DataTable dt)
{
StringBuilder stringBuilder = new StringBuilder();
DataRow[] array = dt.Select(" fatherid=" + parentid);
if (array.Length > 0)
{
for (int i = 0; i < array.Length; i++)
{
stringBuilder.Append("{ ");
int num = Convert.ToInt32(array[i]["pagetypeid"].ToString());
int num2 = Convert.ToInt32(array[i]["isreloadid"].ToString());
stringBuilder.Append("\"id\": \"" + array[i]["ID"].ToString() + "\" ,");
stringBuilder.Append("\"name\": \"" + array[i]["Name"].ToString().Replace("\"", "\\\"") + "\", ");
stringBuilder.Append("\"seq\": \"" + array[i]["Sequence"].ToString() + "\", ");
stringBuilder.Append("\"url\": \"" + array[i]["url"].ToString().Replace("\"", "\\\"") + "\", ");
stringBuilder.Append("\"buttonid\": \"" + array[i]["buttonid"].ToString().Replace("\"", "\\\"") + "\" ,");
stringBuilder.Append(string.Format("\"_parentId\": \"{0}\" ,", parentid));
stringBuilder.Append("\"fatherid\": \"" + parentid + "\" ,");
stringBuilder.Append("\"pagetypename\": \"" + array[i]["pagetypename"].ToString().Replace("\"", "\\\"") + "\" ,");
stringBuilder.Append("\"pagetypeid\": \"" + num + "\" ,");
if (num2 == 1)
{
stringBuilder.Append("\"isreloadid\": \"否\" ,");
}
else
{
stringBuilder.Append("\"isreloadid\": \"是\" ,");
}
stringBuilder.Append("\"state\": \"closed\" ");
stringBuilder.Append("},");
stringBuilder.Append(this.SetMenu(array[i]["ID"].ToString(), dt));
}
}
return stringBuilder.ToString();
}
public string GetJsonFromDataTable(DataTable dt, int total, bool isshowtotal = true)
{
StringBuilder stringBuilder = new StringBuilder();
if (dt.Rows.Count == 0)
{
stringBuilder.Append("{ ");
stringBuilder.Append("\"rows\":[ ");
stringBuilder.Append("]");
if (isshowtotal)
{
stringBuilder.Append(",");
stringBuilder.Append("\"total\":");
stringBuilder.Append(total);
}
stringBuilder.Append("}");
return stringBuilder.ToString();
}
stringBuilder.Append("{ ");
stringBuilder.Append("\"rows\":[ ");
for (int i = 0; i < dt.Rows.Count; i++)
{
stringBuilder.Append("{ ");
for (int j = 0; j < dt.Columns.Count; j++)
{
if (j < dt.Columns.Count - 1)
{
stringBuilder.Append(string.Concat(new string[]
{
"\"",
dt.Columns[j].ColumnName.ToString().ToLower(),
"\":\"",
this.JsonCharFilter(dt.Rows[i][j].ToString()),
"\","
}));
}
else
{
if (j == dt.Columns.Count - 1)
{
stringBuilder.Append(string.Concat(new string[]
{
"\"",
dt.Columns[j].ColumnName.ToString().ToLower(),
"\":\"",
this.JsonCharFilter(dt.Rows[i][j].ToString()),
"\""
}));
}
}
}
if (i == dt.Rows.Count - 1)
{
stringBuilder.Append("} ");
}
else
{
stringBuilder.Append("}, ");
}
}
stringBuilder.Append("]");
if (isshowtotal)
{
stringBuilder.Append(",");
stringBuilder.Append("\"total\":");
stringBuilder.Append(total);
}
stringBuilder.Append("}");
return stringBuilder.ToString();
}
public string JsonCharFilter(string sourceStr)
{
sourceStr = sourceStr.Replace("\\", "\\\\");
sourceStr = sourceStr.Replace("\"", "\\\"");
sourceStr = sourceStr.Replace("\b", "\\b");
sourceStr = sourceStr.Replace("\t", "\\t");
sourceStr = sourceStr.Replace("\n", "\\n");
sourceStr = sourceStr.Replace("\f", "\\f");
sourceStr = sourceStr.Replace("\r", "\\r");
return sourceStr;
}
public string DataTableToFomater(DataTable dt)
{
this.sb.Clear();
this.sb.Append("[{");
if (dt.Rows.Count > 0)
{
string text = dt.Rows[0][0].ToString();
if (text.IndexOf("成功") != -1)
{
this.sb.Append("\"success\":");
this.sb.Append("true,");
}
else
{
this.sb.Append("\"success\":");
this.sb.Append("false,");
}
this.sb.Append("\"msg\":");
this.sb.Append("\"" + text + "\"");
}
this.sb.Append("}]");
return this.sb.ToString();
}
public string CheckLogin()
{
this.sb.Append("[{");
this.sb.Append("\"success\":");
this.sb.Append("false,");
this.sb.Append("\"reload\":");
this.sb.Append("true,");
this.sb.Append("\"url\":");
this.sb.Append("\"/login.html\",");
this.sb.Append("\"msg\":");
this.sb.Append("\"非法操作,请重新登录\"");
this.sb.Append("}]");
return this.sb.ToString();
}
public string GetJsonFromDataTable(DataTable dt, int total, bool ShowFooter, string fields, string inputfiled, string ShowMessage = "合计:", string filter = "")
{
StringBuilder stringBuilder = new StringBuilder();
if (dt.Rows.Count == 0)
{
stringBuilder.Append("{ ");
stringBuilder.Append("\"rows\":[ ");
stringBuilder.Append("]");
stringBuilder.Append(",");
stringBuilder.Append("\"total\":");
stringBuilder.Append(total);
stringBuilder.Append(",\"footer\":[");
stringBuilder.Append("]");
stringBuilder.Append("}");
return stringBuilder.ToString();
}
stringBuilder.Append("{ ");
stringBuilder.Append("\"rows\":[ ");
for (int i = 0; i < dt.Rows.Count; i++)
{
stringBuilder.Append("{ ");
for (int j = 0; j < dt.Columns.Count; j++)
{
if (j < dt.Columns.Count - 1)
{
stringBuilder.Append(string.Concat(new string[]
{
"\"",
dt.Columns[j].ColumnName.ToString().ToLower(),
"\":\"",
this.JsonCharFilter(dt.Rows[i][j].ToString()),
"\","
}));
}
else
{
if (j == dt.Columns.Count - 1)
{
stringBuilder.Append(string.Concat(new string[]
{
"\"",
dt.Columns[j].ColumnName.ToString().ToLower(),
"\":\"",
this.JsonCharFilter(dt.Rows[i][j].ToString()),
"\""
}));
}
}
}
if (i == dt.Rows.Count - 1)
{
stringBuilder.Append("} ");
}
else
{
stringBuilder.Append("}, ");
}
}
stringBuilder.Append("]");
stringBuilder.Append(",");
stringBuilder.Append("\"total\":");
stringBuilder.Append(total);
if (ShowFooter && fields.Length > 0 && inputfiled.Length > 0)
{
stringBuilder.Append(",\"footer\":[{");
stringBuilder.Append(string.Concat(new string[]
{
"\"",
inputfiled,
"\":\"<span style='color:red; font-weight:bold'>",
ShowMessage,
"</span>\","
}));
string[] array = fields.Split(new char[]
{
','
});
for (int k = 0; k < array.Length; k++)
{
if (k < array.Length - 1)
{
stringBuilder.Append(string.Concat(new object[]
{
"\"",
array[k],
"\":\"",
dt.Compute("sum(" + array[k] + ")", filter),
"\","
}));
}
else
{
stringBuilder.Append(string.Concat(new object[]
{
"\"",
array[k],
"\":\"",
dt.Compute("sum(" + array[k] + ")", filter),
"\""
}));
}
}
stringBuilder.Append("}]");
}
stringBuilder.Append("}");
return stringBuilder.ToString();
}
public string GetJsonFromDataTable()
{
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.Append("{ ");
stringBuilder.Append("\"rows\":[ ");
stringBuilder.Append("]");
stringBuilder.Append(",");
stringBuilder.Append("\"total\":");
stringBuilder.Append(0);
stringBuilder.Append(",\"footer\":[");
stringBuilder.Append("]");
stringBuilder.Append("}");
return stringBuilder.ToString();
}
public string ComBoxProductTree(DataTable tables)
{
DataRow[] array = tables.Select(" FatherID=0");
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.Append("{\"total\":" + tables.Rows.Count + ",\"rows\":");
stringBuilder.Append("[ ");
if (array.Length > 0)
{
for (int i = 0; i < array.Length; i++)
{
stringBuilder.Append("{ ");
int num = Convert.ToInt32(array[i]["FatherID"].ToString());
stringBuilder.Append("\"id\": \"" + array[i]["ID"].ToString() + "\" ,");
stringBuilder.Append("\"productid\": \"" + array[i]["ProductID"].ToString().Replace("\"", "\\\"") + "\", ");
stringBuilder.Append("\"productcode\": \"" + array[i]["ProductCode"].ToString().Replace("\"", "\\\"") + "\", ");
stringBuilder.Append("\"productname\": \"" + array[i]["ProductName"].ToString().Replace("\"", "\\\"") + "\" ,");
stringBuilder.Append("\"fatherid\": \"" + num + "\" ,");
stringBuilder.Append("\"qty\": \"" + array[i]["Quantity"].ToString() + "\" ,");
stringBuilder.Append("\"state\": \"closed\" ");
stringBuilder.Append("},");
stringBuilder.Append(this.GetComBoxProductTreeByFatherid(array[i]["ID"].ToString(), tables));
}
}
stringBuilder.Remove(stringBuilder.Length - 1, 1);
stringBuilder.Append("]}");
return stringBuilder.ToString();
}
public string GetComBoxProductTreeByFatherid(string fatherid, DataTable dt)
{
StringBuilder stringBuilder = new StringBuilder();
DataRow[] array = dt.Select(" FatherID=" + fatherid);
if (array.Length > 0)
{
for (int i = 0; i < array.Length; i++)
{
stringBuilder.Append("{ ");
stringBuilder.Append("\"id\": \"" + array[i]["ID"].ToString() + "\" ,");
stringBuilder.Append("\"productid\": \"" + array[i]["ProductID"].ToString().Replace("\"", "\\\"") + "\", ");
stringBuilder.Append("\"productcode\": \"" + array[i]["ProductCode"].ToString().Replace("\"", "\\\"") + "\", ");
stringBuilder.Append("\"productname\": \"" + array[i]["ProductName"].ToString().Replace("\"", "\\\"") + "\" ,");
stringBuilder.Append("\"fatherid\": \"" + fatherid + "\" ,");
stringBuilder.Append("\"qty\": \"" + array[i]["Quantity"].ToString() + "\" ,");
stringBuilder.Append(string.Format("\"_parentId\": \"{0}\" ,", fatherid));
stringBuilder.Append("\"state\": \"closed\" ");
stringBuilder.Append("},");
stringBuilder.Append(this.GetComBoxProductTreeByFatherid(array[i]["ID"].ToString(), dt));
}
}
return stringBuilder.ToString();
}
public string GetProjectCostTreeList(DataTable dt)
{
DataRow[] array = dt.Select(" fatherid=0");
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.Append("{\"total\":" + dt.Rows.Count + ",\"rows\":");
stringBuilder.Append("[ ");
if (array.Length > 0)
{
for (int i = 0; i < array.Length; i++)
{
stringBuilder.Append("{ ");
int num = Convert.ToInt32(array[i]["fatherid"].ToString());
int num2 = Convert.ToInt32(array[i]["projectid"].ToString());
string str = this.JsonCharFilter(array[i]["title"].ToString());
string str2 = this.JsonCharFilter(array[i]["costtypename"].ToString());
decimal num3 = Convert.ToDecimal(array[i]["price"].ToString());
int num4 = Convert.ToInt32(array[i]["costtypeid"].ToString());
stringBuilder.Append("\"id\": \"" + array[i]["ID"].ToString() + "\" ,");
stringBuilder.Append("\"fatherid\": \"" + num + "\" ,");
stringBuilder.Append("\"projectid\": \"" + num2 + "\" ,");
stringBuilder.Append("\"code\": \"" + array[i]["code"].ToString() + "\" ,");
stringBuilder.Append("\"title\": \"" + str + "\" ,");
stringBuilder.Append("\"costtypeid\": \"" + num4 + "\" ,");
stringBuilder.Append("\"costtypename\": \"" + str2 + "\" ,");
stringBuilder.Append("\"price\": \"" + num3 + "\" ,");
stringBuilder.Append("\"state\": \"closed\" ");
stringBuilder.Append("},");
stringBuilder.Append(this.SetProjectMenu(array[i]["ID"].ToString(), dt));
}
}
stringBuilder.Remove(stringBuilder.Length - 1, 1);
stringBuilder.Append("]}");
return stringBuilder.ToString();
}
public string SetProjectMenu(string parentid, DataTable dt)
{
StringBuilder stringBuilder = new StringBuilder();
DataRow[] array = dt.Select(" fatherid=" + parentid);
if (array.Length > 0)
{
for (int i = 0; i < array.Length; i++)
{
stringBuilder.Append("{ ");
int num = Convert.ToInt32(array[i]["fatherid"].ToString());
int num2 = Convert.ToInt32(array[i]["projectid"].ToString());
string str = this.JsonCharFilter(array[i]["title"].ToString());
string str2 = this.JsonCharFilter(array[i]["costtypename"].ToString());
decimal num3 = Convert.ToDecimal(array[i]["price"].ToString());
int num4 = Convert.ToInt32(array[i]["costtypeid"].ToString());
stringBuilder.Append("\"id\": \"" + array[i]["ID"].ToString() + "\" ,");
stringBuilder.Append("\"fatherid\": \"" + num + "\" ,");
stringBuilder.Append("\"projectid\": \"" + num2 + "\" ,");
stringBuilder.Append("\"code\": \"" + array[i]["code"].ToString() + "\" ,");
stringBuilder.Append("\"title\": \"" + str + "\" ,");
stringBuilder.Append("\"costtypeid\": \"" + num4 + "\" ,");
stringBuilder.Append("\"costtypename\": \"" + str2 + "\" ,");
stringBuilder.Append("\"price\": \"" + num3 + "\" ,");
stringBuilder.Append(string.Format("\"_parentId\": \"{0}\" ,", parentid));
stringBuilder.Append("\"state\": \"closed\" ");
stringBuilder.Append("},");
stringBuilder.Append(this.SetAdvancedMenu(array[i]["ID"].ToString(), dt));
}
}
return stringBuilder.ToString();
}
public DataTable CreateDataTable()
{
DataTable dataTable = new DataTable();
dataTable.Columns.Add("abc", typeof(string));
DataRow dataRow = dataTable.NewRow();
object[] itemArray = new object[]
{
"操作失败"
};
dataRow.ItemArray = itemArray;
dataTable.Rows.Add(dataRow);
return dataTable;
}
}
}
C# easyui json类的更多相关文章
- [.net 面向对象程序设计进阶] (13) 序列化(Serialization)(五) Json 序列化利器 Newtonsoft.Json 及 通用Json类
[.net 面向对象程序设计进阶] (13) 序列化(Serialization)(五) Json 序列化利器 Newtonsoft.Json 及 通用Json类 本节导读: 关于JSON序列化,不能 ...
- ecshop json类的使用
ecshop中有2个地方使用了json,一个是cls_json.php文件,一个是transport.js文件. cls_json 封装了json类,可以调用里面的encode的方法,根据参数不同,进 ...
- php递归json类实例代码
这篇文章主要介绍了php递归json类的实现方法,可以实现对索引数组及数字数组的解析,进而实现递归数组返回json字符串的功能.具体实现代码如下: <?php /* * @ anthor:QD ...
- C#代码 json类
using System; using System.Collections.Generic; using System.Collections; using System.Text; using S ...
- C# 转换Json类
using System; using System.Collections.Generic; using System.Text; using System.Data; using System.R ...
- Visual Studio自动生成XML类和JSON类
Visual Studio 2013及以上版本提供了一个非常实用的功能.可以根据xml文档或json文档自动生成类.有了这个工具反序列化时就不用再手动写类并且实现了强类型操作. 步骤 1. 准备一份j ...
- Delphi 10.2 Tokyo新增JSON类学习——TJsonSerializer
Delphi 10.3.2 for windows 7 编译通过,源码下载地址: Tokyo 10.2新增类,效率更高更快 TJsonSerializer 需要引用单元:System.JSON.Ser ...
- 【Mysql】Mysql Json类型或Text类型可以建索引吗?
一.JSON类型 答案是不可以 为Json类型建索引会报错 mysql)); ERROR (): JSON column 'card_pay_data' cannot be used in key s ...
- 【JSON类】使用说明
理解键名路径 键名路径(keyPath)用于定位json的键,比如:{book: {title:”中国人”} },键名路径 book.title 表定位到book下的title键. 对于值是数组类型的 ...
随机推荐
- Linux离线安装pip和numpy
首先说明一下pip在线安装程序会发生什么 例如: 运行pip install numpy 1.pip会先下载与自己机器匹配的wheel安装包 我的是numpy-1.12.1-cp27-cp27mu-m ...
- .net 音频转换 .amr 转 .mp3 (七牛转换法)
.amr 用于移动设备的音频,压缩比比较大,多用于人声.通话,效果还行!所以,移动设备多采用amr格式来进行录存!比较常见的例子:通话录音,微信语音以及录音等! 这个鬼,用两个字来形容,就是“蛋疼”: ...
- MongoDB部署、使用、监控及调优
MongoDB部署 系统环境:CentOS7 下载地址:http://mirrors.163.com/centos/7.6.1810/isos/x86_64/CentOS-7-x86_64-DVD ...
- iOS 利用UIWebView与JavaScript交互的最简单办法
这里说的是针对iOS的!并且方法很简单!!并且验证可行的!!! 1, UIWebView调用 JavaScript 的函数: NSString* strValue = [webView stringB ...
- 安卓&IOS 手机添加O365 邮箱账户
手机添加O365 邮件账户 一.Android手机添加O365邮件账户 1. 找到手机上“电子邮件” 2. 打开设置 3. 点击添加账户 4. 选择“Exchange” 5. 输入O365的邮箱账户和 ...
- CCS3超长文字显示省略号的方法
需求:当文本长度溢出包含元素时以省略号结尾 CSS3实现方法: #MyDIV{overflow:hidden;text-overflow:ellipsis;} 示例:<!DOCTYPE html ...
- swift Equatable 函数签名的测试
struct Degoo:Equatable { var lex:String var pex:String static func == (left:Degoo, right:Degoo) -> ...
- DROP VIEW - 删除一个视图
SYNOPSIS DROP VIEW name [, ...] [ CASCADE | RESTRICT ] DESCRIPTION 描述 DROP VIEW 从数据库中删除一个现存的视图. 执行这条 ...
- Open Cascade创建自己的MFC文档程序
项目初始设置在Visual studio中创建一个单文档MFC项目(本例以MFCTest为名称): 在项目属性的VC++页面设置包含目录.库目录,在链接器的输入中添加OCC库目录下的所有.lib文件名 ...
- PHP20 PHP面向对象辅助
学习要点 常用函数 命名空间 类的自动加载 常用函数 class_exists 作用:检查类是否已定义 语法格式: bool class_exists ( string $class_name [, ...