Datatable converts the data into Json and puts the Json data into the js file. datatablejson
5 code instances in C # converting Datatable to Json
/// <Summary> /// Datatable converted to Json /// </summary> /// <param name = "table"> Datatable object </param> /// <returns> Json string </returns> public static string ToJson (DataTable dt) {StringBuilder jsonString = new StringBuilder (); jsonString. append ("["); DataRowCollection drc = dt. rows; for (int I = 0; I <drc. count; I ++) {jsonString. append ("{"); for (int j = 0; j <dt. columns. count; j ++) {string strKey = dt. C Olumns [j]. columnName; string strValue = drc [I] [j]. toString (); Type type = dt. columns [j]. dataType; jsonString. append ("\" "+ strKey +" \ ":"); strValue = StringFormat (strValue, type); if (j <dt. columns. count-1) {jsonString. append (strValue + ",");} else {jsonString. append (strValue) ;}} jsonString. append ("},");} jsonString. remove (jsonString. length-1, 1); jsonString. append ("]"); return jsonStr Ing. toString ();} /// <summary> /// Format String, date, and Boolean // </summary> /// <param name = "str"> </param>/ // <param name = "type"> </param> // <returns> </returns> private static string StringFormat (string str, type type) {if (type = typeof (string) {str = String2Json (str); str = "\" "+ str + "\"";} else if (type = typeof (DateTime) {str = "\" "+ str +" \ "";} else if (type = typeof (bool )) {str = str. ToLower ();} else if (type! = Typeof (string) & string. isNullOrEmpty (str) {str = "\" "+ str +" \ "";} return str ;} /// <summary> /// filter special characters /// </summary> /// <param name = "s"> string </param> /// <returns> json string </returns> private static String String2Json (string s) {StringBuilder sb = new StringBuilder (); for (int I = 0; I <s. length; I ++) {char c = s. toCharArray () [I]; switch (c) {case '\ "': sb. append ("\" "); break; case '\': sb. append ("\\\\"); break; case '/': sb. append ("\/"); break; case '\ B': sb. append ("\ B"); break; case '\ F': sb. append ("\ f"); break; case '\ N': sb. append ("\ n"); break; case '\ R': sb. append ("\ r"); break; case '\ t': sb. append ("\ t"); break; default: sb. append (c); break;} return sb. toString ();}
<2>
// Query the province name var SQL = "select top 35 locid, locname, parentid from location where loctype = 1"; var dt_pro = SQLHelper. get_DataTable (SQL, SQLHelper. getCon (), new Dictionary <string, DBParam> (); // convert the dt_pro DataTable to json var tt_pro = ToJson (dt_pro ); // query the city name var vsql = "select locid, locname, parentid from location where parentid in (select top 35 locId from location where loctype = 1 )"; var dt_city = SQLHelper. get_DataTable (vsql, SQLHelper. getCon (), new Dictionary <string, DBParam> (); // convert the dt_city DataTable to json var tt_city = ToJson (dt_city );
<3> Create a js file and write two variables var cn_pro in the file. Assign the tt_pro value to cn_pro and the tt_city value to cn_city.
For example, enter var cn_pro = [{"locid": 1, "locname": "Beijing", "parentid": 0 },{ "locid": 673, "locname": "Shanghai", "parentid": 0}];
Var cn_city = [{"locid": 2, "locname": "Beijing", "parentid": 1 },{ "locid": 674, "locname ": "Shanghai", "parentid": 673}]
Of course, this is short for me. Most of the content is removed. The first two data entries are retained as an example.
========================================================== ======================================
<% @ Page Title = "" Language = "C #" MasterPageFile = "~ /Views/Shared/MemberCenter. master "Inherits =" System. web. mvc. viewPage <bigtree. model. memberDetail> "%> <% @ Import Namespace =" bigtree "%> <asp: content ID = "Content1" ContentPlaceHolderID = "MainContent" runat = "server"> <% using (Html. beginForm ()) {%> <div class = "rightCon"> <div class = "nowTil">
C # how to convert datatable data to json
/// <Summary>
/// Convert the datatable to json
/// </Summary>
/// <Param name = "dtb"> Dt </param>
/// <Returns> JSON string </returns>
Public static string Dtb2Json (DataTable dtb)
{
JavaScriptSerializer jss = new JavaScriptSerializer ();
System. Collections. ArrayList dic = new System. Collections. ArrayList ();
Foreach (DataRow dr in dtb. Rows)
{
System. Collections. Generic. Dictionary <string, object> drow = new System. Collections. Generic. Dictionary <string, object> ();
Foreach (DataColumn dc in dtb. Columns)
{
Drow. Add (dc. ColumnName, dr [dc. ColumnName]);
}
Dic. Add (drow );
}
// Serialization
Return jss. Serialize (dic );
}
Jstree is used as a dynamic tree, transmitted in json format, and obtained in the Stored Procedure datatable. I don't know how to convert datatable to the desired json data format.
Datatable is just a table. You can retrieve any data in it after traversing for, and then splice it into a json string.