Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C#
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);
                  //object ndt = newdt;
                  //return serializer.Serialize(ndt);
              }
          }
      }

Please provide your suggestions.
Posted
Updated 7-Jun-14 3:06am
v3

As per my understanding of your question, you would be doing two things:

1. You want to display/store vertical formatted data in somewhere and in your program, you want to write code for that.
2. For convenience, you just want to view data in vertical format.

In second case you just need to use JSON viewer tool:
http://jsonviewer.codeplex.com/releases/view/63839[^]

In first case, you can look into code and have an idea how JSON viewer is doing formatting as it is open source and code is available at:
http://jsonviewer.codeplex.com/SourceControl/latest#Readme.txt[^]

Hope those would help. Thanks.
 
Share this answer
 
v2
try with JSON.NET[^] serializer, it supports JSON formatting

C#
string json= JsonConvert.SerializeObject(rows, Formatting.Indented);

you can install this package via Nuget[^]
 
Share this answer
 

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