0

I have a json string like this: {\"active\": true} and I want replace for this {"active": true}. How can I do that in c#?

I tried jsonString.Replace("\\", ""); jsonString.Replace(@"\", ""); jsonString.Replace(@"\"", "");

nothing worked

  • Are you absolutely sure that the original string contains slashes `\ ` before each quote `"`? – degant May 30 '17 at 18:39
  • Possible duplicate of [Can I expand a string that contains C# literal expressions at runtime](https://stackoverflow.com/questions/3298075/can-i-expand-a-string-that-contains-c-sharp-literal-expressions-at-runtime) – BenCamps May 30 '17 at 19:01
  • 1
    How do you know that you have that backslash as part of the string? Are you looking at the string variable containing the json **using the debugger**? If so then the backslash characters are just the debugger being "helpful" in terms of trying to show you how you would declare a constant in C# to produce that string, but the backslash characters aren't really part of the string. – Lasse V. Karlsen May 30 '17 at 19:09
  • 2
    I am also assuming that you assigned the *result* of calling `Replace` to a string variable? `.Replace` won't *change* the string you call it on, it will return a new string with the changes. ie. this will work: `jsonString = jsonString.Replace(...);`, this will not: `jsonString.Replace(...);` – Lasse V. Karlsen May 30 '17 at 19:11

2 Answers2

0

Since you want replace \" to " you can use Replace.("\\\"", "\"").

You need escape special characters like \ and ". So \ becomes \\, " becomes \", and \" becomes \\\".

Mark Shevchenko
  • 7,937
  • 1
  • 25
  • 29
0

Check Regex.Unescape(string) method. This method returns a new string without escape character.

Akhilesh
  • 171
  • 2
  • 20