C # DataTable to Json implementation

Source: Internet
Author: User

The project uses the Jqgrid front-end data control, the data format requirements for JSON although the system has to do the corresponding implementation, but not to the DataTable to JSON has a corresponding method, so in the project to the DataTable write a go JSON extension method, see clearly is the extension method, remember S Tatic the use of the detailed extension method, please do your own Baidu.

The specific code is as follows:

Public static string datatabletojson ( this DataTable  dt,string  Jsonname)     {        system.text.stringbuilder  json = new system.text.stringbuilder ();         Json.append ("{\" " + jsonName + " \ ": [");         if   (dt. rows.count > 0)         {             for  (Int i = 0; i < dt. rows.count; i++)             {                 json.append ("{");                 for  (int j  = 0; j &Lt; dt. columns.count; j++)                  {                     json.append ("\" " + dt. COLUMNS[J]. Columnname.tostring ()  +  "\": \ ""  + dt. ROWS[I][J]. ToString ()  +  "\" ");                     if  (J < dt. COLUMNS.COUNT&NBSP;-&NBSP;1)                      {                         json.append (",");                     }                 }                 json.append ("}");                 if  (I < dt. ROWS.COUNT&NBSP;-&NBSP;1)                  {                     json.append (",");                 }            }         }        json.append ("]}");         return json.tostring ();     }

C # DataTable to Json implementation

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.