更新时间:2023-09-09 14:28:04
当前,您的Web方法将String
与ResponseFormat = WebMessageFormat.Json
一起返回.它遵循字符串的JSON编码.对应于www.json.org,字符串中的所有双引号都将使用反斜杠转义.因此,您目前具有双重JSON编码.
Currently your web method return a String
together with ResponseFormat = WebMessageFormat.Json
. It follow to the JSON encoding of the string. Corresponds to www.json.org all double quotes in the string will be escaped using backslash. So you have currently double JSON encoding.
返回任何类型数据的最简单方法是将GetCurrentCart()
Web方法的输出类型更改为Stream
或Message
(从System.ServiceModel.Channels
)而不是String
.
参见 http://blogs.msdn.com/b/carlosfigueira/archive/2008/04/17/wcf-raw-programming-model-web.aspx , http://msdn.microsoft.com/en-us/library/cc681221(VS.90).aspx 以获取代码示例.
The easiest way to return any kind of data is to change the output type of GetCurrentCart()
web method to Stream
or Message
(from System.ServiceModel.Channels
) instead of String
.
See http://blogs.msdn.com/b/carlosfigueira/archive/2008/04/17/wcf-raw-programming-model-web.aspx, http://msdn.microsoft.com/en-us/library/ms789010.aspx and http://msdn.microsoft.com/en-us/library/cc681221(VS.90).aspx for code examples.
因为您没有在问题中写下使用的.NET版本,所以建议您使用通用且最简单的方法:
Because you don't wrote in your question which version of .NET you use, I suggest you to use an universal and the easiest way:
public Stream GetCurrentCart()
{
//Code ommited
var j = new { Content = response.Content, Display=response.Display,
SubTotal=response.SubTotal};
var s = new JavaScriptSerializer();
string jsonClient = s.Serialize(j);
WebOperationContext.Current.OutgoingResponse.ContentType =
"application/json; charset=utf-8";
return new MemoryStream(Encoding.UTF8.GetBytes(jsonClient));
}