2015-12-03 45 views
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();  
} 
+1

你確定嗎?或者那只是另一個調試視圖? –

+0

阿米特,它顯示在小提琴手正確。 – Navdeep

+0

嘗試從瀏覽器訪問URL並查看會發生什麼。 –

回答

0

您可以使用此代碼:它需要查詢動態和返回JSON

public List<List<string>> GetDataAsList(string query) 
    { 
     try 
     { 
      intializeConnection(); 



        DataTable dataTable; 
        DataSet dataset = new DataSet(); 
        List<List<string>> data; 
        query = query.Trim().ToUpper(); 
        DataRow row = null; 
        string[] commasCount; 
        string[] commasCountWithoutFrom; 

        if (query.Contains('*')) 
        { 
         return null; 
        } 
        commasCount = query.Trim().Split(new string[] { "FROM" }, StringSplitOptions.None); 
        string commasString = commasCount[0].Trim(); 
        commasCountWithoutFrom = commasString.Trim().Split(','); 
        ArrayList columnsName2 = new ArrayList(); 

        for (int c = 0; c < commasCountWithoutFrom.Length; c++) 
        { 
         string raw = commasCountWithoutFrom[c].Trim(); 
         string[] parts = raw.Trim().Split(' '); 
         string sub_parts = parts[parts.Length - 1]; 
         columnsName2.Add(sub_parts.ToString()); 
        } 


      List<List<string>> resultArr=null; 

      string cmdStr = String.Format(query); 
      command = new SqlCommand(cmdStr, connection); 
      set = new DataSet(); 
      adapter = new SqlDataAdapter(command); 
      adapter.Fill(set); 

      if (set.Tables[0].Rows.Count > 0) 
      { 
       resultArr = new List<List<string>>(); 


       for (int i = 0; i < set.Tables[0].Rows.Count; i++) 
       { 

        resultArr.Add(new List<string>()); 
        for (int col = 0; col < columnsName2.Count; col++) 
        { 

         resultArr[i].Add(set.Tables[0].Rows[i][columnsName2[col].ToString()].ToString().Trim()); 
        } 


       } 
      } 
      else 
      { 

        resultArr = new List<List<string>>(); 
        resultArr.Add(new List<string>()); 
        for (int col = 0; col < columnsName2.Count; col++) 
        { 
         resultArr[0].Add("No Data"); 
        } 



      } 

      connection.Close(); 
      return resultArr; 
     } 
     catch (SystemException ex) 
     { 
      connection.Close(); 
      return null; 
     } 
    } 

並在ISERVICE.cs中使用這一個

[WebInvoke(Method = "GET", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "GetDataAsList/{query}")] 

    List<List<string>> GetDataAsList(string query);