转自 http://www.cnblogs.com/WikStone/archive/2012/07/02/2573137.html
目前项目没有使用第三方的json转换库,都是根据json格式进行字符串拼接。
生成Combobox json格式:
View Code
/// <summary> /// 生成Combobox JSON格式 /// </summary> /// <param name="dt">数据源DataTable</param> /// <returns>Combobox的json格式 [{ "id":1,"text":"text1"}] </returns> public static string CreateJsonDictClass(DataTable dt) { StringBuilder JsonString = new StringBuilder(); //Exception Handling if (dt != null) { JsonString.Append("["); for (int i = 0; i < dt.Rows.Count; i++) { if(i<dt.Rows.Count-1) { JsonString.Append("{ "); JsonString.Append(""id": "); JsonString.Append(dt.Rows[i]["ID"] +","); JsonString.Append(""text": "); JsonString.Append(""" + dt.Rows[i]["Title"]+"""); JsonString.Append("},"); } if (i == dt.Rows.Count - 1) { JsonString.Append("{ "); JsonString.Append(""id": "); JsonString.Append(dt.Rows[i]["ID"] + ","); JsonString.Append(""text": "); JsonString.Append(""" + dt.Rows[i]["Title"] + """); JsonString.Append("}"); } } JsonString.Append("]"); return JsonString.ToString(); } else return null; }
生成DataGrid json格式:
View Code
/// <summary> /// 将DataTable中的数据转换成JSON格式 /// </summary> /// <param name="dt">数据源DataTable</param> /// <param name="displayCount">是否输出数据总条数</param> /// <param name="totalcount">JSON中显示的数据总条数</param> /// <returns></returns> public static string CreateJsonParameters(DataTable dt, bool displayCount, int totalcount) { StringBuilder JsonString = new StringBuilder(); //Exception Handling if (dt != null) { JsonString.Append("{ "); JsonString.Append(""rows":[ "); for (int i = 0; i < dt.Rows.Count; i++) { JsonString.Append("{ "); for (int j = 0; j < dt.Columns.Count; j++) { if (j < dt.Columns.Count - 1) { //if (dt.Rows[i][j] == DBNull.Value) continue; if (dt.Columns[j].DataType == typeof(bool)) { JsonString.Append(""JSON_" + dt.Columns[j].ColumnName.ToLower() + "":" + dt.Rows[i][j].ToString().ToLower() + ","); } else if (dt.Columns[j].DataType == typeof(string)) { JsonString.Append(""JSON_" + dt.Columns[j].ColumnName.ToLower() + "":" + """ + dt.Rows[i][j].ToString().Replace(""", "\"") + "","); } else { JsonString.Append(""JSON_" + dt.Columns[j].ColumnName.ToLower() + "":" + """ + dt.Rows[i][j] + "","); } } else if (j == dt.Columns.Count - 1) { //if (dt.Rows[i][j] == DBNull.Value) continue; if (dt.Columns[j].DataType == typeof(bool)) { JsonString.Append(""JSON_" + dt.Columns[j].ColumnName.ToLower() + "":" + dt.Rows[i][j].ToString().ToLower()); } else if (dt.Columns[j].DataType == typeof(string)) { JsonString.Append(""JSON_" + dt.Columns[j].ColumnName.ToLower() + "":" + """ + dt.Rows[i][j].ToString().Replace(""", "\"") + """); } else { JsonString.Append(""JSON_" + dt.Columns[j].ColumnName.ToLower() + "":" + """ + dt.Rows[i][j] + """); } } } /*end Of String*/ if (i == dt.Rows.Count - 1) { JsonString.Append("} "); } else { JsonString.Append("}, "); } } JsonString.Append("]"); if (displayCount) { JsonString.Append(","); JsonString.Append(""total":"); JsonString.Append(totalcount); } JsonString.Append("}"); return JsonString.ToString().Replace(" ", ""); } else { return null; } }
生成Tree json格式:
View Code
/// <summary> /// 根据DataTable生成Json树结构 /// </summary> /// <param name="tabel">数据源</param> /// <param name="idCol">ID列</param> /// <param name="txtCol">Text列</param> /// <param name="rela">关系字段</param> /// <param name="pId">父ID</param> StringBuilder result = new StringBuilder(); StringBuilder sb = new StringBuilder(); private void GetTreeJsonByTable(DataTable tabel, string idCol, string txtCol, string rela, object pId) { result.Append(sb.ToString()); sb.Clear(); if (tabel.Rows.Count > 0) { sb.Append("["); string filer = string.Format("{0}='{1}'", rela, pId); DataRow[] rows = tabel.Select(filer); if (rows.Length > 0) { foreach (DataRow row in rows) { sb.Append("{"id":"" + row[idCol] + "","text":"" + row[txtCol] + "","state":"open""); if (tabel.Select(string.Format("{0}='{1}'", rela, row[idCol])).Length > 0) { sb.Append(","children":"); GetTreeJsonByTable(tabel, idCol, txtCol, rela, row[idCol]); result.Append(sb.ToString()); sb.Clear(); } result.Append(sb.ToString()); sb.Clear(); sb.Append("},"); } sb = sb.Remove(sb.Length - 1, 1); } sb.Append("]"); result.Append(sb.ToString()); sb.Clear(); } }
调用方法很简单,运行这个void 类型的函数,然后取result 的值就行了,
GetTreeJsonByTable(datatable, "id", "title", "pid", "0");string content = result.ToString();