Click here to Skip to main content
15,881,204 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi everybody, I want to read some load method() to get datas from my DB and store it in .json file.. I have read several articles in google but in every article, they are creating only a json objects, but in my case, I need to store the datas in a separate .json file.. So please help..
Posted
Updated 14-Mar-14 19:41pm
v3

1 solution

Hi Manikandan,

Please use this method to create .json file from your datable...

C#
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...

C#
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);
 
Share this answer
 
v3
Comments
Tejas Vaishnav 15-Mar-14 1:57am    
if it will solve your problem then accept my solution as answer and don't forget to rate it
Member 12636915 8-Feb-17 7:07am    
It is run on my machine. In list there has shown some error. I think your code has some error is there kindly check it. because how i am telling your code has mistake there has no "rows" variable in your coding but you was use that variable in "rows.Add(row);" this line. kindly reply me.
Tejas Vaishnav 14-Feb-17 1:42am    
Sorry for the late reply, but actually if you see the whole code block then you can see the rows variable is declared as List<Dictionary<string, string>>.
Can you please check and let me know if you still have any issue.

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