Convert data in dataset to JSON format

Source: Internet
Author: User

Turn: http://www.cnblogs.com/simonblog/archive/2010/05/12/1733139.html

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Using system. Data;

Namespace consoleapplication1
{
Public static class datasettojson
{
Public static string dstojson (This dataset ds, string jsonname, string [] parname)
{
Try
{
If (DS = NULL)
{
Return "dataset is null, so I can't do it to JSON! ";
}
If (jsonname. Length <1)
{
Return "you set the JSON name is wrong! ";
}
If (Ds. Tables [0]. Columns. Count <parname. length)
{
Return "you give the parname is bigger than dataset columns! ";
}
String josn = "{" + jsonname + ":[";
String temp = "";
For (Int J = 0; j <Ds. Tables [0]. Rows. Count; j ++)
{
Temp = temp + "{";
For (INT I = 0; I <parname. length; I ++)
{
Temp + = "" + parname [I] + ": \ '" + Ds. Tables [0]. Rows [J] [parname [I] + "\'";
If (I! = Parname. Length-1)
{
Temp = temp + ",";
}
}
If (j = Ds. Tables [0]. Rows. Count-1)
{
Temp = temp + "}";
}
Else
{
Temp = temp + "},";
}
}
Josn = josn + temp + "]}";
Return josn;
}
Catch (exception ex)
{
Return "codeing is error ----" + ex. tostring ();
}
}
}
}

 

This is a way to convert the data retrieved into JSON format, and you can also store the data in the database into an ilist <>, then, the jsonwriter in JSON is converted to the JSON format, which is not displayed here.Code,,

 

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.