ZHE.ZHAO
ZHE.ZHAO

Reputation: 385

How to eliminate Escape character from a string

I have a Json saved in database as string:

"{ \"path\" : \"a.crx\", \"uniteDeVolume\" : \"g or ml\", \"unites\" :     \"Acts/Volume\", \"nbIndevMin\" : \"20\", \"nbJours\" : \"6 to 7\", \"ventilDepart\" : \"20051\" }"

When I want to read this field from database, and to deserialize to BsonDocument, the returned value is

"\"{ \\\"path\\\" : \\\"a.crx\\\", \\\"uniteDeVolume\\\" : \\\"g or ml\\\", \\\"unites\\\" : \\\"Acts/Volume\\\", \\\"nbIndevMin\\\" : \\\"20\\\", \\\"nbJours\\\" : \\\"6 to 7\\\", \\\"ventilDepart\\\" : \\\"20051\\\" }\""

How to eleminate the escape characters?

Upvotes: 1

Views: 324

Answers (2)

quickskape
quickskape

Reputation: 46

I would use Regex from 'System.Text.RegularExpressions'

var escapedString ="\"{ \\\"path\\\" : \\\"a.crx\\\", \\\"uniteDeVolume\\\" : \\\"g or ml\\\", \\\"unites\\\" : \\\"Acts/Volume\\\", \\\"nbIndevMin\\\" : \\\"20\\\", \\\"nbJours\\\" : \\\"6 to 7\\\", \\\"ventilDepart\\\" : \\\"20051\\\" }\"";
var unescapedString = Regex.Unescape(escapedString);

returns the following

"{ "path" : "a.crx", "uniteDeVolume" : "g or ml", "unites" : "Acts/Volume", "nbIndevMin" : "20", "nbJours" : "6 to 7", "ventilDepart" : "20051" }"

Upvotes: 2

Related Questions