In this article, we will learn how to convert single DataRow to JSON string in C#. In other words, we can say how to serialize a DataRow to a JSON Format in C#. in this article, we convert the DataRow to JSON string bu using two ways: JavaScriptSerializer
and Linq-to-Json API.
So, for converting the single DataRow
to JSON String, we using JavaScriptSerializer
and Linq-to-Json API. Here, we add a dynamic row to DataTable
for use in this example for converting DataRow
to JSON String or we can use our database to fetch the record and bind into the Datatable. But here we add a dynamic row to our DataTable
using the method given below:
public static DataTable getData()
{
DataTable dt = new DataTable();
dt.Columns.Add("Id", typeof(Int32));
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Designation", typeof(string));
dt.Columns.Add("Location", typeof(string));
dt.Rows.Add(1, "Virat Kohli", "Software Developer", "Delhi");
return dt;
}
In this method, we use JavaScriptSerializer
class which is used for serializing objects into JSON format and deserialize it back to objects and this class belongs to the System.Web.Script.Serialization
namespace. First, we need to do is import the System.Web.Script.Serialization
namespace.
Here is the code to convert DataRow To JSON String using JavaScriptSerializer class in c#
using System;
using System.Collections.Generic;
using System.Data;
using System.Web.Script.Serialization;
namespace Tutorialsrack
{
class Program
{
/* How To Convert Single DataRow To JSON String in C# */
static void Main(string[] args)
{
Console.WriteLine("-- Convert DataRow To JSON String in C# --\n");
string JsonString = Convert_DataRowToJson(getData().Rows[0]);
Console.WriteLine(JsonString);
//Hit ENTER to exit the program
Console.ReadKey();
}
public static DataTable getData()
{
DataTable dt = new DataTable();
dt.Columns.Add("Id", typeof(Int32));
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Designation", typeof(string));
dt.Columns.Add("Location", typeof(string));
dt.Rows.Add(1, "Virat Kohli", "Software Developer", "Delhi");
return dt;
}
public static string Convert_DataRowToJson(DataRow datarow)
{
var dict = new Dictionary<string, object>();
foreach (DataColumn col in datarow.Table.Columns)
{
dict.Add(col.ColumnName, datarow[col]);
}
var jsSerializer = new JavaScriptSerializer();
return jsSerializer.Serialize(dict);
}
}
}
-- Convert DataRow To JSON String in C# --
{"Id":1,"Name":"Virat Kohli","Designation":"Software Developer","Location":"Delhi"}
In this method, we used the LINQ-to-JSON API to build up a JObject
from the DataRow
.
using System;
using System.Data;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Tutorialsrack
{
class Program
{
/* How To Convert Single DataRow To JSON String in C# */
static void Main(string[] args)
{
Console.WriteLine("-- Convert DataRow To JSON String in C# --\n");
string JsonString = Convert_DataRowToJson(getData());
Console.WriteLine(JsonString);
//Hit ENTER to exit the program
Console.ReadKey();
}
public static DataTable getData()
{
DataTable dt = new DataTable();
dt.Columns.Add("Id", typeof(Int32));
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Designation", typeof(string));
dt.Columns.Add("Location", typeof(string));
dt.Rows.Add(1, "Virat Kohli", "Software Developer", "Delhi");
return dt;
}
public static string Convert_DataRowToJson(DataTable dt)
{
string json = new JObject(
dt.Columns.Cast()
.Select(c => new JProperty(c.ColumnName, JToken.FromObject(dt.Rows[0][c])))
).ToString(Formatting.None);
return json;
}
}
}
-- Convert DataRow To JSON String in C# --
{"Id":1,"Name":"Virat Kohli","Designation":"Software Developer","Location":"Delhi"}
I hope this article will help you to understand how to convert DataRow To JSON string in C#.
Share your valuable feedback, please post your comment at the bottom of this article. Thank you!
Comments