C # common datable conversion to JSON, and JSON conversion to DataTable operation method

Source: Internet
Author: User
Tags instance method string format tojson

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.