Convert DataSet / DataTable to JSON array using C# in ASP.Net MVC

Convert DataSet / DataTable to JSON array using C# in ASP.Net MVC

 

In this article, you will how to Convert DataSet / DataTable to JSON array using C# in ASP.Net MVC.

 

 So, simplest way to convert a dataset to a JSON array.

 

You can use Newtonsoft Json dll.

 

using Newtonsoft.Json;

 

public static string datasettojson(DataSet ds) {
    return JsonConvert.SerializeObject(ds, Formatting.Indented);
}

 

public static T convertdatasettoJson<T>(this System.Data.DataSet dataSet)
{
    try
    {
        XmlDocument doc = new XmlDocument();
        doc.LoadXml(dataSet.GetXml());
        return (T)Convert.ChangeType(JsonConvert.SerializeXmlNode(doc).Replace("null", "\"\"").Replace("'", "\'"), typeof(T));
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

2021-10-03