Hi Manikandan,
Please use this method to create .json file from your datable...
public bool WriteJason(DataTable dt, string path)
{
try
{
System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
List<Dictionary<string, string>> rows = new List<Dictionary<string, string>>();
Dictionary<string, string> row = null;
foreach (DataRow dr in dt.Rows)
{
row = new Dictionary<string, string>();
foreach (DataColumn col in dt.Columns)
{
row.Add(col.ColumnName.Trim().ToString(), Convert.ToString(dr[col]));
}
rows.Add(row);
}
string jsonstring = serializer.Serialize(rows);
using (var file = new StreamWriter(path, false))
{
file.Write(jsonstring);
file.Close();
file.Dispose();
}
return true;
}
catch { return false; }
}
and you can call this function like this...
DataTable dt = new DataTable("Student");
dt.Columns.Add("ID");
dt.Columns.Add("Name");
dt.Columns.Add("RollNo");
for (int i = 0; i < 5; i++)
{
DataRow dr = dt.NewRow();
dr["ID"] = i.ToString();
dr["Name"] = "ABC" + i.ToString();
dr["RollNo"] = "RN" + i.ToString();
dt.Rows.Add(dr);
}
dt.AcceptChanges();
string filePath = @"D:\test.json";
WriteJason(dt, filePath);