0

Imagine that I have this json:

{
  "a":"b"
}

If I have a function in C++ that returns this json in string format, such as std::string getjson(), it would return stringified version of this json like:

"{\"a\":\"b\"}"

To be complete:

std::string getjson()
{
    return "{\"a\":\"b\"}";
}

myfavourite_jsonparser.parse(getjson());

This works.

Now imagine that the value of key in json contains a ":

{
  "a":"\"b"
}

How could I return this from std::string getjson() function?

Mert Mertce
  • 1,049
  • 7
  • 34

1 Answers1

3

Just as you escape a quote with a backslash (\" results in "), a backslash can be used to escape backslashes as well. So \\ results in \.

So if you want to output \", then escape like this: \\\".

tenfour
  • 36,141
  • 15
  • 83
  • 142