JSON and the DataTable (DataSet) transform each other

Source: Internet
Author: User

public static string Createjsonparameters (DataTable dt)
{
/* /****************************************************************************
* Without goingin to the depth of the functioning of this Method, I'll try to give an overview
* As soon as this method gets a DataTable it starts to convert it into JSON String,
* It takes each row and with each row it grabs the cell name and its data.
* This kind of JSON was very usefull when developer has to have the Column name of the.
* Values Can is Access on the clien in the this. Obj. Head[0].<columnname>
* Note:one negative point. By this method, user'll is not being able to call any cell by its index.
* *************************************************************************/
StringBuilder jsonstring = new StringBuilder ();
Exception Handling
if (dt! = null && dt. Rows.Count > 0)
{
Jsonstring.append ("{");
Jsonstring.append ("\" head\ ": [");
for (int i = 0; i < dt. Rows.Count; i++)
{
Jsonstring.append ("{");
for (int j = 0; j < dt. Columns.count; J + +)
{
if (J < dt. COLUMNS.COUNT-1)
{
Jsonstring.append ("\" "+ dt. COLUMNS[J]. Columnname.tostring () + "\": "+" \ "" + dt. ROWS[I][J]. ToString () + "\", ");
}
else if (j = = dt. COLUMNS.COUNT-1)
{
Jsonstring.append ("\" "+ dt. COLUMNS[J]. Columnname.tostring () + "\": "+" \ "" + dt. ROWS[I][J]. ToString () + "\" ");
}
}
/*end of string*/
if (i = = dt. ROWS.COUNT-1)
{
Jsonstring.append ("}");
}
Else
{
Jsonstring.append ("},");
}
}
Jsonstring.append ("]}");
return jsonstring.tostring ();
}
Else
{
return null;
}
}

JSON and the DataTable (DataSet) transform each other

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.