Sunday, 16 September 2018

Convert C# Data table into JSON format. (VS2010, Newtonsoft JSON)

There are 3 Ways to Convert DataTable to JSON String in ASP.NET C#
  1. Convert DataTable to JSON using StringBuilder.
  2. Convert DataTable to JSON using JavaScriptSerializer.
  3. Convert DataTable to JSON using JSON.Net DLL.

Method 1
Convert DataTable to JSON using StringBuilder. 
This is how the JSON sample data looks: {"firstName":"Chandra", "lastName":"Kant"}.
JSON objects are written inside curly braces and can contain multiple name/values pairs.
So using StringBuilder we can create a similar JSON Structured String.
Since we are using StringBuilder we need to import the System.Text namespace in our page as in the following:


Using System.Text;  



The following code will generate a JSON string. Here we are making a for loop over our DataTable rows and columns. Fetch the data (values) and append it to our JSONString StringBuilder. This is how our code looks:

public string DataTableToJSONWithStringBuilder(DataTable table)   

{  
    var JSONString = new StringBuilder();  
    if (table.Rows.Count > 0)   
    {  
        JSONString.Append("[");  
        for (int i = 0; i < table.Rows.Count; i++)   
        {  
            JSONString.Append("{");  
            for (int j = 0; j < table.Columns.Count; j++)   
            {  
                if (j < table.Columns.Count - 1)   
                {  
                    JSONString.Append("\"" + table.Columns[j].ColumnName.ToString() + "\":" + "\"" + table.Rows[i][j].ToString() + "\",");  
                }   
                else if (j == table.Columns.Count - 1)   
                {  
                    JSONString.Append("\"" + table.Columns[j].ColumnName.ToString() + "\":" + "\"" + table.Rows[i][j].ToString() + "\"");  
                }  
            }  
            if (i == table.Rows.Count - 1)   
            {  
                JSONString.Append("}");  
            }   
            else   
            {  
                JSONString.Append("},");  
            }  
        }  
        JSONString.Append("]");  
    }  
    return JSONString.ToString();  
}  


-----------------------------------------------------------------------------------------------


Method 2

Convert the DataTable to JSON using JavaScriptSerializer.
Since we are using JavaScriptSerializer we first need to import the System.Web.Script.Serialization namespace into our page as in the following code:



using System.Web.Script.Serialization; 



The JavaScriptSerializer class is used internally by the asynchronous communication layer to serialize and deserialize the data.

To serialize an object, use the Serialize method. To deserialize a JSON string, use the Deserialize or DeserializeObject methods.



Here we use the serialize method to get the JSON format data. So our code looks as in the following:

public string DataTableToJSONWithJavaScriptSerializer(DataTable table) 
{  
    JavaScriptSerializer jsSerializer = new JavaScriptSerializer();  
    List < Dictionary < stringobject >> parentRow = new List < Dictionary < stringobject >> ();  
    Dictionary < stringobject > childRow;  
    foreach(DataRow row in table.Rows) 
    {  
        childRow = new Dictionary < stringobject > ();  
        foreach(DataColumn col in table.Columns) 
        {  
            childRow.Add(col.ColumnName, row[col]);  
        }  
        parentRow.Add(childRow);  
    }  
    return jsSerializer.Serialize(parentRow);  
}  


-----------------------------------------------------------------------------------------------


Method 3


Convert DataTable to JSON using JSON.Net DLL (Newtonsoft).


Now in this method, we will convert our C# Datatable to JSON using the Newtonsoft DLL.

For this first, we need to download JSON.Net DLL. We can download it from Nuget.org and then import the Newtonsoft.JSON namespace into our page as in the following code. JSON.NET is a popular high-performance JSON framework for .NET.

using Newtonsoft.JSON;  


public string DataTableToJSONWithJSONNet(DataTable table)
 {  
   string JSONString=string.Empty;  
   JSONString = JSONConvert.SerializeObject(table);  
   return JSONString;  
}  
Yes we are done with JSON,


No comments:

Post a Comment

SQL Table : Change Row As Column and Group Them... ( Setting column values as column names in the SQL query result )

Setting column values as column names in the SQL query result Problem Statement : id col1 col2 1 Pending 30 1 Resolved 48 ...