如何从字符串中消除转义字符

时间:2015-12-16 13:04:43

标签: c#

我在数据库中保存了Json字符串:

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

当我想从数据库中读取此字段并反序列化为BsonDocument时,返回的值为

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

如何消除转义字符?

2 个答案:

答案 0 :(得分:2)

我会使用'System.Text.RegularExpressions'

中的Regex
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);

返回以下内容

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

答案 1 :(得分:0)

相关问题