Replacing escape characters from JSON

2019-01-20 10:30发布

问题:

I want to replace the "\" character from a JSON string by a empty space. How can I do that?

回答1:

I have found that the easiest and best way to remove all escape characters from your JSON string, is to pass the string into Regex.Unescape() method. This method returns a new string with no ecapes, even \n \t etc, are removed.

See this MSDN article for more details: Regex.Unescape Method (String) (System.Text.RegularExpressions)



回答2:

Regex.Unescape() method will work just fine in most cases, but some particular cases require custom replacements. E.g. Regex.Unescape() will generate actual line breaks which are not supported in JSON.

Unescaped JSON:

{"comments_count":27,"comments":"<a name=\"comments\"><\/a>\n\n\t\n\t\n\t\t\n\t<div class=\"CommentsList\">\n\t\t<strong>COMENTARII<\/strong>\n\t\t"}

Regex.Unescape

{"comments_count":27,"comments":"<a name="comments"></a>

Replacements
    <div class="CommentsList">
        <strong>Comments</strong>
"}

Custom replacements

private string SanitizeReceivedJson(string uglyJson)
{
    var sb = new StringBuilder(uglyJson);
    sb.Replace("\\\t", "\t");
    sb.Replace("\\\n", "\n");
    sb.Replace("\\\r", "\r");
    return sb.ToString();
}

{"comments_count":27,"comments":"<a name=\"comments\"><\/a>\n\n\t\n\t\n\t\t\n\t<div class=\"CommentsList\">\n\t\t<strong>COMENTARII<\/strong>"}


回答3:

If the json object is a string, in .Net the escape "\" characters are added, should you want to clean up the json string, JObject.Parse({string}) as demostrated in the following code snippet cleans up nicely:

var myJsonAsString = "{\"Name\": \"John\", \"LastName\": \"Doe\", \"Age\": 199 }";

var myCleanJsonObject = JObject.Parse(myJsonAsString);

Should give us a clean Json object with the escape characters removed.

{
"Name": "John",
"LastName": "Doe",
"Age": 199
}


回答4:

C# string assignment does that for you, although if name or value contains \ it will be double escaped. The proper way is to use variable.Replace("\\\\","\\"); or variable.Replace(@"\\",@"\"); This will remove double escaped \ character, Leaving REQUIRED \ in value. for example if JSON contains "Domain\Username" this will be returned as \"Domain\\\\Username\" assigning that to string will result you will have Domain\\Username



回答5:

Just grab the json string and make it a String.Replace()

string JsonContentFixed = JsonContentString.Replace(@"\", " ");


回答6:

try this

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


回答7:

Basically you are asking how to replace the backslash in C#.

You have to use String.Replace method. The documentation tell us:

String.Replace Method (String, String)

Returns a new string in which all occurrences of a specified string in the current instance are replaced with another specified string.

So, your string will take the value returned by Replacemethod:

jsonString= jsonString.Replace(@"\"," ");


回答8:

You can use Replace()

string variable = "{\"data\": {\"id\": \"1\",\"name\": \"jon\"}}";
Console.WriteLine(variable.Replace("\\", " "));