title: How to serialize specific Json object from C#
body: <p>I have a requirement where i need to serialize <code>json</code> object in below format </p>
  
  <pre><code>[{
      "columns": [{
          "title": "NAME"
      }, {
          "title": "COUNTY"
      }],
      "data": [
          ["John Doe", "Fresno"],
          ["Billy", "Fresno"],
          ["Tom", "Kern"],
          ["King Smith", "Kings"]
      ]
  }]
  </code></pre>
  
  <p>Here i need to get this json object from two different source, one is <code>Columns</code> and other is <code>data</code>. Columns would come from a <code>string</code> which will be comma separated as</p>
  
  <pre><code>string columnNames = "Name, County";
  </code></pre>
  
  <p>and <code>data</code> would come from .net Datatable like</p>
  
  <pre><code>DataTable dt = new DataTable();
  </code></pre>
  
  <p>I tried with below code using <code>JavaScriptSerializer</code> but i am not able to format it in the required format. Actually, shared format is required to dynamically create jquery datatable. Here is my raw code in C#.</p>
  
  <pre><code>[WebMethod]
  public static string ConvertDatadttoString(string appName)
  {
  
      DataTable dt = new DataTable();
  
      dt.Columns.Add("Name", typeof(string));
      dt.Columns.Add("County", typeof(string));
  
      dt.Rows.Add("vo", "go.com");
      dt.Rows.Add("pa", "pa.com");
  
      System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
      List&lt;Dictionary&lt;string, object&gt;&gt; rows = new List&lt;Dictionary&lt;string, object&gt;&gt;();
      Dictionary&lt;string, object&gt; row;
      foreach (DataRow dr in dt.Rows)
      {
          row = new Dictionary&lt;string, object&gt;();
          foreach (DataColumn col in dt.Columns)
          {
              row.Add(col.ColumnName, dr[col]);
          }
          rows.Add(row);
      }
      return serializer.Serialize(rows);
  }
  </code></pre>
  
  <p>Above code is only serializing the <code>DataTable</code> and is not able to create in the required format. Thanks.</p>