简体   繁体   English

如何从C#中的数据库中获得\\的双引号值

[英]How to get double quoted value with out \ from the database in c#

I am fetching some string value from the database which should have double quotes, for example: 我正在从数据库中获取一些应该用双引号引起来的字符串值,例如:

"DoubleQuotes"

but when ever it comes to the c# part above string value becomes: 但是当涉及到c#部分时,字符串值变为:

\"DoubleQuotes\"

I have even tried using replace method but it takes out the \\ but also removes " from everywhere. 我什至尝试使用replace方法,但它取出了\\但也从任何地方都删除了"

Code: 码:

repository = new Repository();
string genericTemplate = Convert.ToString(@repository.GetTemplateWithoutFormulas(sheetName)); // this returns the string value from the db
return genericTemplate;

O/p on the browser I get is: 我得到的浏览器上的O / p是:

"{\"version\":\"9.40.20153.0\",\"sheetCount\":2,\"sheets\":{\"Sheet1\":{\"name\":\"Sheet1\",\"selections\":{\"0\":{\"row\":1,\"rowCount\":1,\"col\":1,\"colCount\":1}},\"rowCount\":200,\"columnCount\":20,\"activeRow\":1,\"activeCol\":1,\"theme\":\"Office\",\"rowHeaderData\":{\"defaultDataNode\":{\"style\":{\"themeFont\":\"Body\"}}},\"colHeaderData\":{\"defaultDataNode\":{\"style\":{\"themeFont\":\"Body\"}}},\"data\":{\"dataTable\":{\"0\":{\"0\":{\"value\":\"daman\",\"style\":{\"autoFormatter\":{\"customerCultureName\":\"en-US\"}}},\"1\":{\"value\":\"sandhu\",\"style\":{\"autoFormatter\":{\"customerCultureName\":\"en-US\"}}}},\"1\":{\"0\":{\"value\":\"hello\",\"style\":{\"autoFormatter\":{\"customerCultureName\":\"en-US\"}}},\"1\":{\"value\":\"chu\",\"style\":{\"autoFormatter\":{\"customerCultureName\":\"en-US\"}}}}},\"defaultDataNode\":{\"style\":{\"themeFont\":\"Body\"}}},\"index\":0},\"Sheet2\":{\"name\":\"Sheet2\",\"selections\":{\"0\":{\"row\":0,\"rowCount\":1,\"col\":0,\"colCount\":1}},\"rowCount\":200,\"columnCount\":20,\"activeRow\":0,\"activeCol\":0,\"theme\":\"Office\",\"index\":1}}}"

Your GetTemplateWithoutFormulas() method doesn't just return a string, it returns a JSON string of a JSON object. 您的GetTemplateWithoutFormulas()方法不仅返回字符串,还返回JSON对象的JSON字符串。 Apparently you stringified one too many times. 显然您将字符串串了太多遍了。 You need to parse out that JSON string and get the string representation of that result. 您需要解析该JSON字符串并获取该结果的字符串表示形式。

I don't know what libraries you're using but assuming Json.NET, you could do something like this: 我不知道您在使用什么库,但是假设使用Json.NET,您可以执行以下操作:

var template = repository.GetTemplateWithoutFormulas(sheetName);
var token = JToken.Parse(template);
return token.ToString();

You should also consider making that method return a JSON object instead, rather than a JSON string of a JSON object. 您还应该考虑使该方法返回一个JSON对象,而不是JSON对象的JSON字符串。 Then you wouldn't have to do this in the first place. 这样一来,您就不必这样做了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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