1Convert #region DataTable to JSON string instance method2///<summary>3///Summary description of GETCLASSTYPEJOSN4///</summary>5PublicClassGetclasstypejosn:ihttphandler6{7///<summary>8///File name: DataTable and JSON string mutual transfer9///All rights reserved: Copyright (C) Create Family Wealth LIANGJW10///Create a Logo: 2013-08-0311///</summary>12//Usage Description Instance13PublicvoidProcessRequest (HttpContext context)14{The context. Response.ContentType ="Application/json";The context. Response.Charset ="Utf-8";HttpRequest req =Context. Request;18String method = req["Method"]. Tostr (). ToLower ();192021st//Get Contract Details List DataTable converted to JSON string22if (method = ="Txtdate")23{24String json ="";BO. MAKECONTRACTMX BLL =NewMAKECONTRACTMX ();DataSet ds =Bll. Getdatatable ();27if (ds. Tables.count >0)28{JSON =tojson (ds. tables[0]);30}31Context. Response.Write (JSON);32Return;33}3435}3637PublicboolIsReusable38{39Get40{41ReturnFalse;42}43}44}4546#endregion4748#region JSON string conversion to a DataTable instance method4950PublicDataTable jsontodatatable (JSON)51{dt= DataTableTodatatable (JSON);53ReturnDt54}5556#endregion5758Convert #region DataTable to JSON string59///<summary>60///DataTable object converted to JSON string61///</summary>62///<param name= "DT" ></param>63///<returns></returns>64PublicStaticString ToJson (ThisDataTable DT)65{JavaScriptSerializer JavaScriptSerializer =NewJavaScriptSerializer ();Javascriptserializer.maxjsonlength = Int32.MaxValue;//Get the maximum valueArrayList ArrayList =NewArrayList ();69foreach (DataRow DataRowInchDt. Rows)70{dictionary<Stringobject> dictionary =New dictionary<StringObject> ();//Instantiate a collection of parameters72foreach (DataColumn DataColumnInchDt. Columns) The dictionary. ADD (Datacolumn.columnname, Datarow[datacolumn.columnname]. Tostr ()); Arraylist.add (dictionary); Add key value in ArrayList collection (javascriptserializer.serialize) (arrayList); Returns a JSON string to the "Bayi #endregion" the #region JSON string converted to a DataTable data set.//<summary>//JSON string to Datat Able data Collection//</summary> <param name= "JSON" ></param>//<returns></returns> 89 public static DataTable Todatatable (the This string json) all (): DataTable = new DataTable ();//instantiation of the DataTable R Esult; 94 {JavaScriptSerializer JavaScriptSerializer = new JavaScriptSerializer (); Gth = Int32.MaxValue; Obtain the maximum value of ArrayList ArrayList = javascriptserializer.deserialize<arraylist> (JSON); 98 if (Arraylist.count > 0) (dictionary<string, object> Dictionary in ArrayList) 101 {102 if (dict Ionary. keys.count<string> () = = 0) 103 {104 result = Datatable;105 return result;106}107 if (DataTable.Columns.Count = = 0) 108 {109 foreach (string current in dictionary. Keys) {111 DataTable.Columns.Add (current, dictionary[current]. GetType ());}113}114 DataRow datarow = Datatable.newrow (); Keys) (117 datarow[current] = dictionary[current];118}119-DATATABLE.ROWS.ADD (DataRow); Loop add line to dataTable 121}122}123}124 catch125 {126}127 result = datatable;128 return result;129}130 #endregion131 #r Convert Egion to String String type 133//<summary>134///Convert to string String type 135//</summary>136//<param name= "S" > Gets the value that needs to be converted </param>137//<param name= "format" > number of bits to format </param>138///<returns> Returns a new string < /returns>139 public static string Tostr (This object s, string format = ""), 141 string result = ""; 142 try143 {144 I F (format = = "") 145 {146 result = s.tostring (); 147}148 else149 {"result = string. Format ("{0:" + format + "}", s); 151}152}153 catch154 {155}156 retUrn result;157}158 #endregion
Conversion of DataTable and JSON to each other