2017-03-16 171 views
-1

我是從確定年代從SQL的本地服務器接收這樣的數據看起來像這樣刪除雙引號從Json的接收數據表中的C#

「{‘USER_NAME’:‘ADAM2’,‘電子郵件’:」 [email protected]」, 'FIRST_NAME': '亞當阿納斯'}」

public class UsersController : ApiController 
{ 

    public string openconn(string strQuery) 
    { 
     SqlConnection conn = new SqlConnection("Data Source=SAMIB-20042\\SQLEXPRESS;Initial Catalog=SSOwebAPI;Integrated Security=True"); 
     conn.Open(); 
     // Create a command to extract the required data and assign it the connection string 
     SqlCommand cmd = new SqlCommand(strQuery, conn); 
     cmd.CommandType = CommandType.Text; 
     // Create a DataAdapter to run the command and fill the DataTable 
     SqlDataAdapter da = new SqlDataAdapter(); 
     da.SelectCommand = cmd; 
     DataTable dt = new DataTable(); 
     da.Fill(dt); 
     conn.Close(); 
     return GetJson(dt); 
    } 

    public string GetJson(DataTable dt) 
    { 
     System.Web.Script.Serialization.JavaScriptSerializer serializer = new 
     System.Web.Script.Serialization.JavaScriptSerializer(); 
     List<Dictionary<string, object>> rows = 
      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.Trim(), dr[col]); 
      } 
      rows.Add(row); 
     } 
     return serializer.Serialize(rows); 
    } 

    public string GetName(string Username) 
    { 
     string strQuery = @"USE SSOwebAPI;select USER_NAME,EMAIL,FIRST_NAME,LAST_NAME,FIRST_NAME_AR,LAST_NAME_AR,MOBILE_NUMBER,USER_ACCOUNT_ID,CREATION_DATE,UAE_ID_NUMBER,PASSPORT_NO,LICENSE_NUMBER,NAME_EN,NAME_AR,CASE_PARTY_ID FROM [dbo].[Table] WHERE USER_NAME =" + "'" + Username + "'"; 
     string resulw = openconn(strQuery); 
     string outputjson = resulw.Replace("[", string.Empty).Replace("]", string.Empty); 
     string outputjsona = outputjson.Replace("\"", string.Empty).Replace("'", string.Empty); 


     return outputjsona; 

    } 

} 

}

我試圖與r EMOVE雙引號開頭,但失敗了,我也試圖使JSON格式

// GlobalConfiguration.Configuration.Formatters.JsonFormatter.MediaTypeMappings.Add(
// new QueryStringMapping("type", "json", new MediaTypeHeaderValue("application/json"))); 

但雙引號在開始時不能被刪除

,請幫助我,我堅持,因爲我無法驗證JSON這樣我可以在我的Android手機上使用

謝謝

+0

不要發送垃圾郵件標籤。 – Olaf

+0

如果你刪除'''你的字符串將不是一個有效的json btw。試試這樣:'jsonString.Replace(「\」「,」「)。Replace(」'「,」\「」); ' – uTeisT

回答

0

使用String.Trim方法來刪除特定的前導和結束符號:

String value = "\"My value in quotes\""; 
value = value.Trim('\"'); // Quotes removed