-4

I have a problem removing the char \ from a string containing multiple of these chars.

What i do is:

        string oldString = "{\"data\": [{\"name\": \"Test User Two\",\"id\": \"138488723160636\"}]}";
        string newString = oldString.Replace(@"\", "");

This does not work. On the other hand the following does work:

        string test = "fafeaeaetrt";
        string newString = test.Replace(@"a", "");

Besides this i have tried almost every solution in the following thread, nothing works: Remove '\' char from string c#

I know that there is something that i must have misunderstood about this :)

Can you tell me what and how i remove the character \ ?

EDIT:

But the JSON does not seem to work. I have a unit test that returns this json when the method _fbClient.Client.Get("/me/friends"); is called. See below

        dynamic friendListData = _fbClient.Client.Get("/me/friends");

        foreach (dynamic friend in friendListData.data)
        {
            friendsFacebookIds.Add(Convert.ToInt64(friend.id));
        }

When i try to access the data i get the exception message: 'string' does not contain a definition for 'data'

Thought it had something to do with the backslashes since this JSON validates to be valid using a online validator and removing the backslashes.

Community
  • 1
  • 1
Diemauerdk
  • 5,238
  • 9
  • 40
  • 56
  • 4
    Your string doesn't contain any backslashes anyway. They're only in the source to escape the quotes. – Jon Skeet Aug 28 '15 at 11:34
  • Don'y replace anything, your json is correct. Try `var obj = JObject.Parse(oldString);` – Eser Aug 28 '15 at 11:35
  • First explain the "Does not work part". What happens, what do you expect to happen ? ( copy paste some output.) However, your `oldString` does not contain any \ characters, the \ characters you have in the code are used to escape the double quotes. – nos Aug 28 '15 at 11:35
  • Set a breakpoint and look at what's actually in `oldString` after that assignment occurs. The ```\``` character is used to escape the `"` so they appear literally in the string, rather than being used to "close" the string. – sab669 Aug 28 '15 at 11:40
  • 2
    Add a line to output your string at the console to verify your string if the \" confuse you. `Console.Out.WriteLine(newString);` – Spell Aug 28 '15 at 11:41
  • now that is a separate question - you need to use a JSON parser! – Daniel A. White Aug 28 '15 at 11:58

3 Answers3

6

There is no \ character in your string, just " character that need to be escape with \.

Martin Delille
  • 11,360
  • 15
  • 65
  • 132
3

Those are escaping " by the compiler. They aren't in the string data itself. This is true even for the Visual Studio debugger - you might see them there.

If that didn't exist, the compiler would treat it as the end of the string literal.

You can read up on C#'s string escaping.

Now if the string literal was prefixed with @, that would mean that an actual \ is there.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
1

This doesnt work because in the oldstring \ is used as escape sequence. So, there is no \ character in oldstring

Sateesh Pagolu
  • 9,282
  • 2
  • 30
  • 48