Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a dataset named dSet1.

I want to convert it to a json object.

How can I will do it?
Posted
Comments
Bernhard Hiller 6-Mar-14 3:05am    
The process is called "Serialization", and you'll need a Serializer which can serialize to JSON format, e.g. JavaScriptSerializer or DataContractJsonSerializer.

You just use the below method
C#
public string GetJson(DataSet ds)
{
    System.Web.Script.Serialization.JavaScriptSerializer serializer = new

    System.Web.Script.Serialization.JavaScriptSerializer();
    List> rows =
      new List>();
    Dictionary row = null;

    foreach (DataRow dr in ds.Tables[0].Rows)
    {
        row = new Dictionary();
        foreach (DataColumn col in ds.Tables[0].Columns)
        {
            row.Add(col.ColumnName.Trim(), dr[col]);
        }
        rows.Add(row);
    }
    return serializer.Serialize(rows);
}
 
Share this answer
 
v2
C#
DataSet ds = new DataSet();
XmlDataSource xmlDataSource = new XmlDataSource();
xmlDataSource.ID = "XmlSource1";
xmlDataSource.EnableCaching = false;

ds = objSDO.GetMethod(1);

xmlDataSource.Data = ds.GetXml();

XmlDocument doc = new XmlDocument();
doc.LoadXml(xmlDataSource.Data);
var json = JsonConvert.SerializeXmlNode(doc);

it will convert ds into json object
 
Share this answer
 
v2
Comments
Bernhard Hiller 6-Mar-14 3:02am    
What's objSDO, and what is that GetMethod?
$*Developer - Vaibhav*$ 6-Mar-14 3:04am    
fill your dataset

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900