DataTable and DataSet Transfer JSON

Source: Internet
Author: User

usingSystem;usingSystem.Collections.Generic;usingSystem.Data;usingSystem.Linq;usingSystem.Text;usingsystem.web;usingSystem.Web.Script.Serialization;/// <summary>///Summary description of JsonConvert/// </summary> Public classjsonconvert{#regionDataTable converted to JSON format/// <summary>          ///DataTable converted to JSON format/// </summary>          /// <param name= "DT" ></param>          /// <returns></returns>           Public Static stringToJson (DataTable dt) {JavaScriptSerializer Jsserializer=NewJavaScriptSerializer (); List<Dictionary<string,Object>> ParentRow =Newlist<dictionary<string,Object>>(); Dictionary<string,Object>Childrow; foreach(DataRow rowinchdt. Rows) {Childrow=Newdictionary<string,Object>(); foreach(DataColumn Colinchdt. Columns) {Childrow.add (col.            ColumnName, Row[col]);        } parentrow.add (Childrow); }        returnjsserializer.serialize (ParentRow); }    #endregionDataTable converted to JSON format#regionDataSet into JSON format/// <summary>          ///DataSet into JSON format/// </summary>          /// <param name= "ds" >DataSet</param>          /// <returns></returns>           Public Static stringToJson (DataSet ds) {StringBuilder json=NewStringBuilder (); foreach(DataTable DTinchds. Tables) {json. Append ("{\""); Json. Append (dt.            TableName); Json. Append ("\":"); Json.            Append (ToJson (DT)); Json. Append ("}"); }        returnJSON.    ToString (); }    #endregion}

DataTable and DataSet Transfer JSON

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.