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`:
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);
return json;
}
else
{
string json ="null";
return json;
}
}
catch (Exception 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`:
[ServiceContract]
public interface IGetDeviceId
{
[OperationContract()]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "GetDeviceId")]
string GetDeviceIds();
}