如何从Restful WCF服务发送JSON响应时摆脱转义(“\”,“\ n”等等...)字符 [英] How to get rid of escape ("\","\n" etc..) characters in while sending a JSON response from Restful WCF Service
问题描述
在我的WCF服务中,我有一个以JSON格式返回响应的服务方法。但是当我调用该服务时,我得到了JSON响应,里面有很多转义字符。我理解这就是C#表示带双引号的字符串的方式。但是以某种方式或其他方式我希望得到一个干净的Json作为回应。
我当前的回应是;
{GetMDXResponseResult:{\:[{\cubeDatabaseName \:\Adventure Works DW 2008R2 \,\cubeName \ :\Adventure Works \,\cubeBaseName \:\\},{\cubeDatabaseName \:\Adventure Works DW 2008R2 \,\cubeName \:\Mined Customers \,\cubeBaseName \:\\}]}}
但我希望得到我的回复;
{GetMDXResponseResult:[{cubeDatabaseName:Adventure Works DW 2008R2,cubeName :Adventure Works},{cubeDatabaseName:Adventure Works DW 2008R2,cubeName:Mined customers}]}
我们将非常感谢您对此的任何帮助。
谢谢;
Bhaskar
In my WCF Service, I have a service method which returns response in JSON format. But when I call the service I get the JSON response with lots of escape characters inside it. I understand that this the way C# represents a string with double quotes. But in some or other way I want to get a clean Json as a response.
My Current response is;
{"GetMDXResponseResult":"{\" : [{\"cubeDatabaseName\" : \"Adventure Works DW 2008R2\",\"cubeName\" : \"Adventure Works\",\"cubeBaseName\" : \"\"},{\"cubeDatabaseName\" : \"Adventure Works DW 2008R2\",\"cubeName\" : \"Mined Customers\",\"cubeBaseName\" : \"\"}]}"}
But I want to have my response as;
{"GetMDXResponseResult":[{"cubeDatabaseName":"Adventure Works DW 2008R2","cubeName":"Adventure Works"},{"cubeDatabaseName":"Adventure Works DW 2008R2","cubeName":"Mined Customers"}]}
Any help on this will be highly appreciated.
Thanks;
Bhaskar
推荐答案
Nor mal JSON没有引号转义,除非它们在引用区域内,你可能在调试器中查看这个,其中c#编码是为字符串等做的。
如果有是转义然后JSON无效。
Normal JSON does not have escapes for quotes unless they are within quoted areas, you are probably viewing this in the debugger where c# encoding is done for strings etc.
If there are escapes then the JSON is invalid.
这篇关于如何从Restful WCF服务发送JSON响应时摆脱转义(“\”,“\ n”等等...)字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!