0
從sql server數據庫獲取數據我能夠從SQL Server表中獲取json數據,但json的格式似乎不正確。從json使用wcf,c#
有多餘的「」 JSON後:
"[{\"deviceid\":\"jafjajf17841278947\"},
{\"deviceid\":\"ahfaj2528\"},
{\"deviceid\":\"hefhsf9872987572\"},
{\"deviceid\":\"22\"},
{\"deviceid\":\"23\"}]"
在年底和JSON 現在問題開始
引號的是,當我們把這個JSON就是所謂的Android和瀏覽器,我們得到這些倒引號,但不在提琴手。 我需要對代碼進行更改。
代碼我想:
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);
//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
:
[ServiceContract]
public interface IGetDeviceId
{
[OperationContract()]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "GetDeviceId")]
string GetDeviceIds();
}
你確定嗎?或者那只是另一個調試視圖? –
阿米特,它顯示在小提琴手正確。 – Navdeep
嘗試從瀏覽器訪問URL並查看會發生什麼。 –