Convert #region DataTable to JSON string instance method
<summary>
Summary description of GETCLASSTYPEJOSN
</summary>
public class Getclasstypejosn:ihttphandler
{
<summary>
File name: DataTable and JSON string mutual transfer
All rights reserved: Copyright (C) Create Family Wealth LIANGJW
Create a Logo: 2013-08-03
</summary>
Usage Description Instance
public void ProcessRequest (HttpContext context)
{
Context. Response.ContentType = "Application/json";
Context. Response.Charset = "Utf-8";
HttpRequest req = context. Request;
String method = Req["Method"]. Tostr (). ToLower ();
Get Contract Details List DataTable converted to JSON string
if (method = = "Txtdate")
{
String json = "";
BO. MAKECONTRACTMX BLL = new MAKECONTRACTMX ();
DataSet ds = BLL. Getdatatable ();
if (ds. Tables.count > 0)
{
JSON =tojson (ds. Tables[0]);
}
Context. Response.Write (JSON);
Return
}
}
public bool IsReusable
{
Get
{
return false;
}
}
}
#endregion
#region JSON string conversion to a DataTable instance method
Public DataTable jsontodatatable (JSON)
{
DataTable dt= todatatable (JSON);
return DT;
}
#endregion
Convert #region DataTable to JSON string
<summary>
DataTable object converted to JSON string
</summary>
<param name= "DT" ></param>
<returns></returns>
public static string ToJson (this DataTable DT)
{
JavaScriptSerializer JavaScriptSerializer = new JavaScriptSerializer ();
Javascriptserializer.maxjsonlength = Int32.MaxValue; Get the maximum value
ArrayList ArrayList = new ArrayList ();
foreach (DataRow datarow in dt. Rows)
{
dictionary<string, object> Dictionary = new dictionary<string, object> (); Instantiate a collection of parameters
foreach (DataColumn DataColumn in dt. Columns)
{
Dictionary. ADD (Datacolumn.columnname, Datarow[datacolumn.columnname]. Tostr ());
}
Arraylist.add (dictionary); Adding a key value to the ArrayList collection
}
Return Javascriptserializer.serialize (arrayList); Returns a JSON string
}
#endregion
Convert #region Json string to DataTable data collection
<summary>
Json string converted to DataTable data collection
</summary>
<param name= "JSON" ></param>
<returns></returns>
public static DataTable todatatable (This string json)
{
DataTable datatable = new DataTable (); Instantiation of
DataTable result;
Try
{
JavaScriptSerializer JavaScriptSerializer = new JavaScriptSerializer ();
Javascriptserializer.maxjsonlength = Int32.MaxValue; Get the maximum value
ArrayList ArrayList = javascriptserializer.deserialize<arraylist> (JSON);
if (Arraylist.count > 0)
{
foreach (dictionary<string, object> Dictionary in ArrayList)
{
if (dictionary. keys.count<string> () = = 0)
{
result = dataTable;
return result;
}
if (DataTable.Columns.Count = = 0)
{
foreach (String current in dictionary. Keys)
{
DATATABLE.COLUMNS.ADD (current, dictionary[current]. GetType ());
}
}
DataRow datarow = Datatable.newrow ();
foreach (String current in dictionary. Keys)
{
Datarow[current] = dictionary[current];
}
DATATABLE.ROWS.ADD (DataRow); Loop to add rows to the DataTable
}
}
}
Catch
{
}
result = dataTable;
return result;
}
#endregion
#region converted to string string type
<summary>
Convert to String type
</summary>
<param name= "S" > get the value to convert </param>
<param name= "format" > number of bits to format </param>
<returns> returns a new string </returns>
public static string Tostr (This object s, string format = "")
{
string result = "";
Try
{
if (format = = "")
{
result = S.tostring ();
}
Else
{
result = String. Format ("{0:" + format + "}", s);
}
}
Catch
{
}
return result;
}
#endregion
C # common datable conversion to JSON, and JSON conversion to DataTable operation method