从JSON中仅删除\ [英] Remove only \ from JSON
问题描述
我正在从数据库查询中获取一个JSON字符串,该查询从多个表中选择数据并返回记录列表,然后将JSON制成
I am getting a JSON string from a database query which selects data from multiple tables and return a list of records and then making JSON as,
var result = db.Database.SqlQuery<JobNoQ>(query).ToList();
string json = JsonConvert.SerializeObject(result);
然后我得到以下JSON:
and then I am getting following JSON:
"[{\"S1\":\"1\",\"S2\":\"202010010\",\"S30\":\"COCA COLA BEVERAGES PAKISTAN LTD. GUJRANWALA\",\"S31\":\"Coca cola 1\",\"D1\":\"2015-07-01T00:00:00\",\"S5\":\"001\",\"S32\":\"Local\",\"S6\":\"T1\",\"S33\":\"By Road\",\"S10\":\"\"},
{\"S1\":\"3\",\"S2\":\"202010010\",\"S30\":\"COCA COLA BEVERAGES PAKISTAN LTD. GUJRANWALA\",\"S31\":\"Coca cola 1\",\"D1\":\"2015-07-01T00:00:00\",\"S5\":\"002\",\"S32\":\"Innter City\",\"S6\":\"T1\",\"S33\":\"By Road\",\"S10\":\"\"},
{\"S1\":\"4\",\"S2\":\"202010010\",\"S30\":\"COCA COLA BEVERAGES PAKISTAN LTD. GUJRANWALA\",\"S31\":\"Coca cola 1\",\"D1\":\"2015-07-01T00:00:00\",\"S5\":\"003\",\"S32\":\"International\",\"S6\":\"T2\",\"S33\":\"By Sea\",\"S10\":\"\"}
]"
我要从此数组中删除\.我尝试过以下操作:
I want to remove \ from this array. I have tried following:
1. string json = json.Replace("\"", " "); // It removes \ and all "" from the array. I only want to remove the \
2. string json = json.Replace(@"\", " "); // It does nothing
推荐答案
您的JSON中没有反斜杠,这就是为什么用" "
替换@"\"
无效的原因.调试器会很有帮助,但会令人困惑,将JSON字符串转换为C#字符串语法,这意味着调试器将在您看到的任何"
之前添加\
.如果实际上让您的代码打印JSON字符串,您会发现它们不存在.
Your JSON doesn't have any backslashes in it, that's why replacing @"\"
by " "
has no effect. Your debugger is helpfully but confusingly translating your JSON string to the C# string syntax, which means your debugger is adding \
before any "
you see. If you actually let your code print the JSON string, you'll see they aren't there.
这篇关于从JSON中仅删除\的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!