c-sharp

DataTable to JSON

daReportCard obj = new daReportCard();
                DataTable dt = obj.Load_Summary(year, month);

                System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
                List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
                Dictionary<string, object> row;
                foreach (DataRow dr in dt.Rows)
                {
                    row = new Dictionary<string, object>();
                    foreach (DataColumn col in dt.Columns)
                    {
                        row.Add(col.ColumnName, dr[col]);
                    }
                    rows.Add(row);
                }

                return Json(serializer.Serialize(rows), JsonRequestBehavior.AllowGet);
            }
Was this helpful?