Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am able to get json data from a SQL Server table but the format of json seems to be in incorrect.

There is extra " " after json:

"[{\"deviceid\":\"jafjajf17841278947\"},
{\"deviceid\":\"ahfaj2528\"},
{\"deviceid\":\"hefhsf9872987572\"},
{\"deviceid\":\"22\"},
{\"deviceid\":\"23\"}]"

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.

Code I tried:

`GetDeviceId.svc.cs`:

C#
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 > 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<Dictionary<string, object>> rowsList = new List<Dictionary<string, object>>();
       Dictionary<string, object> row = null;

       foreach (DataRow dr in dt.Rows)
       {
           row = new Dictionary<string, object>();

           foreach (DataColumn col in dt.Columns)
           {
               row.Add(col.ColumnName, dr[col]);
           }

           rowsList.Add(row);
       }

       return Jserializer.Serialize(rowsList);
   }

`IGetDeviceId.svc`:

C#
[ServiceContract]
    public interface IGetDeviceId
    {
        [OperationContract()]
        [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "GetDeviceId")]
        string GetDeviceIds();       
    }
Posted
Updated 3-Dec-15 18:15pm
v2
Comments
John C Rayan 3-Dec-15 11:10am    
Did you check your db output first before sending as JSON. Debug and check.
Navdeep Singh 3-Dec-15 11:51am    
Yes i had ,
Link To Service : gps.traxistar.net/GetDeviceId.svc/GetDeviceId
This is the link but i donnot need "" at extreme ends of json.

1 solution

 
Share this answer
 
v3

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