public string ConvertDataTabletoJson()
{
DataTable table = new DataTable();
table.Columns.Add("Dosage", typeof(int));
table.Columns.Add("Drug", typeof(string));
table.Columns.Add("Patient", typeof(string));
table.Columns.Add("Date", typeof(DateTime));
//
// Here we add five DataRows.
//
table.Rows.Add(25, "Indocin", "David", DateTime.Now);
table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now);
table.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now);
table.Rows.Add(21, "Combivent", "Janet", DateTime.Now);
table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now);
string[] jsonArray = new string[table.Columns.Count];
string headString = string.Empty;
for (int i = 0; i < table.Columns.Count; i++)
{
jsonArray[i] = table.Columns[i].Caption; // Array for all columns
headString += "'" + jsonArray[i] + "' : '" + jsonArray[i] + i.ToString() + "%" + "',";
}
headString = headString.Substring(0, headString.Length - 1);
StringBuilder sb = new StringBuilder();
sb.Append("[");
if (table.Rows.Count > 0)
{
for (int i = 0; i < table.Rows.Count; i++)
{
string tempString = headString;
sb.Append("{");
// To get each value from the datatable
for (int j = 0; j < table.Columns.Count; j++)
{
tempString = tempString.Replace(table.Columns[j] + j.ToString() + "%", table.Rows[i][j].ToString());
}
sb.Append(tempString + "},");
}
}
else
{
string tempString = headString;
sb.Append("{");
for (int j = 0; j < table.Columns.Count; j++)
{
tempString = tempString.Replace(table.Columns[j] + j.ToString() + "%", "-");
}
sb.Append(tempString + "},");
}
sb = new StringBuilder(sb.ToString().Substring(0, sb.ToString().Length - 1));
sb.Append("]");
return sb.ToString(); // json formated output
}
{
DataTable table = new DataTable();
table.Columns.Add("Dosage", typeof(int));
table.Columns.Add("Drug", typeof(string));
table.Columns.Add("Patient", typeof(string));
table.Columns.Add("Date", typeof(DateTime));
//
// Here we add five DataRows.
//
table.Rows.Add(25, "Indocin", "David", DateTime.Now);
table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now);
table.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now);
table.Rows.Add(21, "Combivent", "Janet", DateTime.Now);
table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now);
string[] jsonArray = new string[table.Columns.Count];
string headString = string.Empty;
for (int i = 0; i < table.Columns.Count; i++)
{
jsonArray[i] = table.Columns[i].Caption; // Array for all columns
headString += "'" + jsonArray[i] + "' : '" + jsonArray[i] + i.ToString() + "%" + "',";
}
headString = headString.Substring(0, headString.Length - 1);
StringBuilder sb = new StringBuilder();
sb.Append("[");
if (table.Rows.Count > 0)
{
for (int i = 0; i < table.Rows.Count; i++)
{
string tempString = headString;
sb.Append("{");
// To get each value from the datatable
for (int j = 0; j < table.Columns.Count; j++)
{
tempString = tempString.Replace(table.Columns[j] + j.ToString() + "%", table.Rows[i][j].ToString());
}
sb.Append(tempString + "},");
}
}
else
{
string tempString = headString;
sb.Append("{");
for (int j = 0; j < table.Columns.Count; j++)
{
tempString = tempString.Replace(table.Columns[j] + j.ToString() + "%", "-");
}
sb.Append(tempString + "},");
}
sb = new StringBuilder(sb.ToString().Substring(0, sb.ToString().Length - 1));
sb.Append("]");
return sb.ToString(); // json formated output
}
No comments:
Post a Comment