Common C # classes

Source: Internet
Author: User
Using System;  Using System.Collections.Generic;  Using System.Data;  Using System.Linq;  Using System.Text;        Using System.Web; ////// Datatableconvertjson Summary Description ///public class Datatableconvertjson {public Datatableconvertjson () {/////TODO: Add constructor logic here } #region DataTable into JSON format//// DataTable converted to JSON format ////////
 
  
         public static string Datatable2json (DataTable dt) {StringBuilder Jsonbuilder = new StringBuilder ();          Jsonbuilder.append ("{\" "); Jsonbuilder.append (dt.          TableName);          Jsonbuilder.append ("[");          Jsonbuilder.append ("["); for (int i = 0; i < dt. Rows.Count;              i++) {jsonbuilder.append ("{"); for (int j = 0; j < dt. Columns.count;                  J + +) {jsonbuilder.append ("\" "); Jsonbuilder.append (dt. COLUMNS[J].                  ColumnName);                  Jsonbuilder.append ("\": \ ""); Jsonbuilder.append (dt. ROWS[I][J].                  ToString ());              Jsonbuilder.append ("\", ");              } jsonbuilder.remove (jsonbuilder.length-1, 1);          Jsonbuilder.append ("},");          } jsonbuilder.remove (jsonbuilder.length-1, 1);          Jsonbuilder.append ("]");          Jsonbuilder.append ("}"); return jsonbuilder.tostring ();      } #endregion DataTable into JSON format #region DataSet into JSON format//  /// DataSet converted to JSON format /////DataSet///
 
  
 public static string Dataset2json (DataSet ds) {StringBuilder json = new StringBuilder (); foreach (DataTable dt in DS. Tables) {json.              Append ("{\" "); Json. Append (dt.              TableName); Json.              Append ("\": "); Json.              Append (Datatable2json (DT)); Json.          Append ("}"); } return JSON.      ToString (); } #endregion///// MSDN ///////////
 
  
       public static string Datatabletojson (String jsonname, DataTable dt) {StringBuilder Json = new STRINGB          Uilder ();          Json.append ("{\" "+ Jsonname +" \ ": ["); if (dt. Rows.Count > 0) {for (int i = 0; i < dt. Rows.Count;                  i++) {json.append ("{"); for (int j = 0; j < dt. Columns.count; J + +) {json.append ("\" + dt. COLUMNS[J]. Columnname.tostring () + "\": \ "" + dt. ROWS[I][J].                      ToString () + "\" "); if (J < dt.                      columns.count-1) {json.append (",");                  }} json.append ("}"); if (i < dt.                  rows.count-1) {json.append (",");          }}} json.append ("]}");      return json.tostring (); }  }
  • Related Article

    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.