从数据表c#中删除Json接收的双引号

时间:2017-03-16 20:31:37

标签: c# android json

我从SQL本地服务器的datable接收这样的数据就像这样

  
    

" {' USER_NAME':' adam2',' EMAIL':' adam2@gmail.com',& #39; FIRST_NAME':' adam anas'}"

  
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;

    }

}

}

我试图在开头删除双引号但是失败了,我也尝试制作json格式化程序

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

但是开头的双引号无法删除

请帮助我,我被卡住了,因为我无法验证json所以我可以在我的Android手机中使用它

谢谢

1 个答案:

答案 0 :(得分:0)

使用String.Trim方法删除特定的前导符号和结束符号:

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