skip to Main Content

I have the following controller:

public IActionResult Get()
{
    var res = GetResult();
    // res is a JSON {"hello": "howareyou"}
    return Ok(res);
}

I’m getting in the result:

{"hello": "how\are\you"}

How to remove the double slash from the reusult? Why they’re being added?

3

Answers


  1. Why they’re being added?

    Because that’s how you print a backslash in JSON. is a special character. It pairs with the next character(s) to define what it prints. For example, n means to add a new line, not printing "n". \ means a print a single backslash.

    The server (your code) and the client using your application will understand this escape sequence just fine.

    Login or Signup to reply.
  2. You don’t need to remove them since you have them in json already. If you want to get rid of them fix json of the action

    var res = GetResult();
    res.Replace("\"," ");
     return Ok(res);
    
    Login or Signup to reply.
  3. These are not part of the string but only one i.e. is part of string. another is added as a escape character only.

    if you print this you will only get single slash not double these are just representational value at the time of debug only.

    Check live code : here

    enter image description here

    Escape Characters

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search