Click here to Skip to main content
15,923,789 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I want to remove back slash fro the below json data
{ \"filed\": \"0d5bd590-86b5-4281-9ef7-924c15190273\", \"FirstName\": \"1sssqwer\", \"LastName\": \"sss2\", \"EmailId\": \"ssss3@infor.com\", \"ClientPrincipal\": \"0d5bd590-86b5-4281-9ef7-924c15190273\"}

but that will not removed

What I have tried:

string s = @"{[]}";
var result = s;

var data = JsonConvert.SerializeObject(result);
string[] tokens = data.Split(',');

userRole = userRole.Replace("\"", "");
Posted
Updated 31-Jul-19 0:43am
Comments
Member 13964245 21-Jun-19 8:03am    
Hi, Iam facing the same problem, but can't find proper solution. have you resolved it? If yes, kindly share the correct solution

The answer is simple: there is no backslash in the string!

What you're seeing is the Visual Studio debugger's representation of the string. Since you're using C#, it displays the string with the escape characters you would need to include to type that value as a string literal in your C# code. But those escape characters are not actually part of the string.

If you use the immediate window, type: ?data,nq and you should see the contents of the string without the escape characters.
 
Share this answer
 
Approach 1: use string.replace

json.Replace(@"\", " ")


Approach 2: use Regex.Unescape

var regex = new Regex(pattern);

data = Regex.Unescape(data);
 
Share this answer
 
Thanks Richard, I was seeing this in VS debug and didn't register it was VS doing it.
The thing that threw me was testing my REST service results using Chrome RESTLet client extension was showing responses from my service with the \ as well.
Tried YARC extension and that result did not include the \
strange!
 
Share this answer
 
Comments
Richard MacCutchan 14-Nov-18 11:40am    
Also you can get that if you try to encode a string of JSON into JSON a second time.
line.Replace(@"\", string.Empty);

or
line.Replace(@"\", "");
 
Share this answer
 
Comments
CHill60 30-Jan-19 4:00am    
Adds nothing new to the previously posted solutions. Besides - this won't work as there is no backslash in the string. See the accepted solution
var json = JsonConvert.SerializeObject(error);
return Request.CreateResponse(status, JsonConvert.DeserializeObject(json));
 
Share this answer
 
Comments
Dave Kreskowiak 17-Apr-19 7:53am    
An unexplained code snippet isn't really an answer. Also, the backslash characters are not really in the JSON file.
if you use
JsonConvert.SerializeObject

in your code it will add backslash by default, and you can remove it by playing it with string.

But, its better to create a class file and return it directly.

Strictly, in case of webapi
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900