2017-03-16 142 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