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,,