title: get data from sql server database in json using wcf,c#
body: <p>I am able to get json data from a SQL Server table but the format of json seems to be in incorrect.</p>
  
  <p>There is extra " " after json:</p>
  
  <pre><code>"[{\"deviceid\":\"jafjajf17841278947\"},
   {\"deviceid\":\"ahfaj2528\"},
   {\"deviceid\":\"hefhsf9872987572\"},
   {\"deviceid\":\"22\"},
   {\"deviceid\":\"23\"}]" 
  </code></pre>
  
  <p>Inverted commas at end and beginning of json
  Now problem is that when we call this json is called in android and browser we get these inverted quotes but not in fiddler.
  Do i need to make changes in code.</p>
  
  <p>Code I tried:</p>
  
  <p><code>GetDeviceId.svc.cs</code>:</p>
  
  <pre><code>public string GetDeviceIds()
  {
      try
      {
          MySqlCommand command = default(MySqlCommand);
          MySqlDataAdapter adaptor = new MySqlDataAdapter();
  
          DataTable dt = new DataTable();
          string sql = "select  deviceid from userreg";
  
          digitalindia.Open();
          command = new MySqlCommand(sql, digitalindia);
          adaptor.SelectCommand = command;
  
          adaptor.Fill(dt);
          adaptor.Dispose();
  
          command.Dispose();
          digitalindia.Close();
  
          if (dt.Rows.Count &gt; 0)
          {
               string json = GetJson(dt);
               //var j = JsonConvert.SerializeObject(json);
               //j = j.Substring(2);
               //json
               //string json = GetJson(dt);
               //json.Remove(1, json.Length - 1);
               //var json1 = EvaluateException(json);
               //ArrayList json = new ArrayList();
               //ArrayList json = new ArrayList();
               //json.Add(DataTableToJsonWithStringBuilder(dt));
               return json;
               //return string.Format("You entered: {0}", json); ;
           }
           else
           {
               string json ="null";
               return json;
               //return "No Party Found";
           }
       }
       catch (Exception ex)
       {
           //ArrayList json = new ArrayList();
           //return ex;
           return ex.Message.ToString();
       }
  }
  
  private string GetJson(DataTable dt)
  {
      System.Web.Script.Serialization.JavaScriptSerializer Jserializer = new System.Web.Script.Serialization.JavaScriptSerializer();
      List&lt;Dictionary&lt;string, object&gt;&gt; rowsList = new List&lt;Dictionary&lt;string, object&gt;&gt;();
      Dictionary&lt;string, object&gt; row = null;
  
      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]);
          }
  
          rowsList.Add(row);
      }
  
      return Jserializer.Serialize(rowsList);
  }
  </code></pre>
  
  <p><code>IGetDeviceId.svc</code>:</p>
  
  <pre><code>[ServiceContract]
  public interface IGetDeviceId
  {
      [OperationContract()]
      [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "GetDeviceId")]
      string GetDeviceIds();       
  }  
  </code></pre>