【发布时间】:2017-06-27 14:07:32
【问题描述】:
我有这份合同:
[ServiceContract]
public interface IServiceJsonContract
{
[WebInvoke(UriTemplate = "/MyMethod", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, Method = "POST")]
Message MyMethod(Message input);
}
MyMethod 的定义是:
Message MyMethod(Message input)
{
...
Message response = Message.CreateMessage(
MessageVersion.None,
"*",
"{\"bla\": 2 }",
new DataContractJsonSerializer(typeof(string)));
response.Properties.Add( WebBodyFormatMessageProperty.Name,new
WebBodyFormatMessageProperty(WebContentFormat.Json));
var contextOutgoingResponse = WebOperationContext.Current.OutgoingResponse;
contextOutgoingResponse.ContentType = "application/json;charset=utf-8";
return response;
}
调用方法时,我得到了 Json 转义:
"{\"bla\": 2 }"
而不是未转义的(如下):
"{"bla": 2 }"
知道如何获取未转义的 Json ("{"bla": 2 }") 吗?
谢谢
【问题讨论】:
-
您应该传递字典或对象,而不是构建自己的 JSON。
-
谢谢,SLaks。你能提供一个简短的例子吗?(在我的情况下,我有一个字符串,DataContractJsonSerializer 也是字符串,但这会逃避响应