简体   繁体   中英

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?

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" }"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM