I need to create a JSON file in C#. The data for JSON file comes form SQL Server. I want to convert a dt(dataTable object) to JSON file in formatted manner.
I am using below code for this but output is in below format(horizontal):
[{"Hotel_Name":"Hotel1","filter_board_id":1,"filter_holiday_type_id":3,"filter_airport_from_iata":"FFF","filter_airport_to_iata":"SFS"},
But I want it to be in vertical format like below:
[{"Hotel_Name":"Hotel1",
"filter_board_id":1,
"filter_holiday_type_id":3,
"filter_airport_from_iata":"FFF",
"filter_airport_to_iata":"SFS"
},
I an using below code as of now:
public string ConvertDataTabletoString()
{
DataTable dt = new DataTable();
using (SqlConnection con = new SqlConnection("Data Source=mypc;Initial Catalog=db;Persist Security Info=True;User ID=sa;Password=password@123;pooling=true;Min Pool Size=20;Max Pool Size=200;Connect Timeout=120"))
{
using (SqlCommand cmd = new SqlCommand("select * from paradism_publisher where Hotel_Name='Hotel1'", con))
{
con.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
DataTable newdt = PivotTable(dt);
System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
List<dictionary><string,>> rows = new List<dictionary><string,>>();
Dictionary<string,> row;
foreach (DataRow dr in newdt.Rows)
{
row = new Dictionary<string,>();
foreach (DataColumn col in newdt.Columns)
{
row.Add(col.ColumnName, dr[col]);
}
rows.Add(row);
}
return serializer.Serialize(rows);
}
}
}
Please provide your suggestions.