Asp.net datatable and dataset are serialized into JSON format

Source: Internet
Author: User

Datatabletojson:

Using system; using system. collections. generic; using system. LINQ; using system. text; using system. runtime. serialization. JSON; using system. runtime. serialization; using system. io; using system. web. script. serialization; using microblog. common. apihelper; using system. web; namespace microblog. common. apihelper {public class resolve {// <summary> // convert JSON text to an object, generic method // </Summary> /// <typeparam name = "T"> type </typeparam> /// <Param name = "jsontext"> JSON text </param> // <returns> specifies the type of object </returns> Public static t jsontoobject <t> (string jsontext, httpcontext context) {javascriptserializer JSS = new javascriptserializer (); try {return JSS. deserialize <t> (jsontext);} catch (exception ex) {return default (t );}} /// <summary> /// convert the JSON text to a data row // </Summary> /// <Param name = "jsontext"> JSON text </param> /// <returns> data row dictionary </returns> Public static dictionary <string, object> datarowfromjson (string jsontext, httpcontext context) {return jsontoobject <dictionary <string, Object> (jsontext, context );}}}

Call: dictionary <string, Object> dt = resolve. datarowfromjson (requestbuffer, context );

Datasettojson:

public class DataSetConverter : JsonConverter{    public override bool CanConvert(Type objectType)    {        return typeof(DataSet).IsAssignableFrom(objectType);    }    public override void WriteJson(JsonWriter writer, object value)    {        DataSet ds = (DataSet)value;        writer.WriteStartObject();        foreach (DataTable dt in ds.Tables)        {            writer.WritePropertyName(dt.TableName);            writer.WriteStartArray();            foreach (DataRow dr in dt.Rows)            {                writer.WriteStartObject();                foreach (DataColumn dc in dt.Columns)                {                    writer.WritePropertyName(dc.ColumnName);                    writer.WriteValue(dr[dc].ToString());                }                writer.WriteEndObject();            }            writer.WriteEndArray();        }        writer.WriteEndObject();    }}

Call: javascriptconvert. serializeobject (gettestdataset (), new datasetconverter ());


Datatabletojson2:

public class DataTableConverter : JsonConverter    {        public override void WriteJson(JsonWriter writer, object value)        {            DataTable dt = (DataTable)value;            writer.WriteStartArray();            foreach (DataRow dr in dt.Rows)            {                writer.WriteStartObject();                foreach (DataColumn dc in dt.Columns)                {                    writer.WritePropertyName(dc.ColumnName);                    writer.WriteValue(dr[dc].ToString());                }                writer.WriteEndObject();            }            writer.WriteEndArray();        }        public override bool CanConvert(Type objectType)        {            return typeof(DataTable).IsAssignableFrom(objectType);        }    }

Call: javascriptconvert. serializeobject (gettestdatatable (), new datatableconverter ());

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.