0

I need to remove slash from my output json. The following function i am using in my API controller. But still i am getting the slash in result. How can i remove this slash?

 public string GetEmployeeDetails(string AccessCard)
    {
        DataTable dt =GetEmployeeByAccessCard(AccessCard);
        if (dt.Rows.Count>0)
        {
            string JSONresult;
            JSONresult = JsonConvert.SerializeObject(dt);
            string outputjson = JSONresult.Replace("\\", "");
            return outputjson;

        }
        else
            return "No Data found";

    }
Vahid
  • 99
  • 1
  • 2
  • 14
  • You can refer to this link and adapt regular expression : https://forums.asp.net/t/1432771.aspx?RegEx+to+remove+double+slashes+from+the+Url+s – Mirko Acimovic May 28 '18 at 05:59
  • its not working – Vahid May 28 '18 at 07:16
  • @Vahid any specific reason for removing slash from json? What particular functionality are you looking to achieve? – Mohsin Mehmood May 28 '18 at 07:59
  • i created a web API. I need to get json in proper format as a result of this function. But what i am getting is look like this. _"{\"ID\":\"3411\",\"AccessCardNo\":\"123569\",\"DP_EmpID\":\"63\",\"EmpOfficialID\":\"EMP020\",\"emp.DP_Degree\":\"710\",\"emp.DP_DepartmentID\":\"699\",\"emp.DP_EmpFname\":\"Feras\",\"ActualDepartureDate\":\"01/01/2099\",\"EmpStatus_HR\":\"Reserved\"}"_ – Vahid May 28 '18 at 08:44
  • Is this what the web browser is showing you or are you seeing this in the debugger ? Check this link also https://stackoverflow.com/questions/20312974/newtonsoft-json-serializeobject-without-escape-backslashes – Storm May 28 '18 at 09:33

2 Answers2

0

In your json string two thing is wrong. Json starts with " and ends with " First trim these double quotation. Then replace \\. Like

string outputjson=JSONresult.Trim('"');
outputjson = JSONresult.Replace("\\", "");
Nagib Mahfuz
  • 833
  • 10
  • 19
0

Try this code:

public string GetEmployeeDetails(string AccessCard)
    {
        DataTable dt =GetEmployeeByAccessCard(AccessCard);
        if (dt.Rows.Count>0)
        {
            string result= Convert.ToString(dt);
            string JSONresult = JsonConvert.SerializeObject(result.Replace("\\", ""));
            return JSONresult;
        }
        else
            return "No Data found";

    }
Sahil Sharma
  • 1,813
  • 1
  • 16
  • 37