【发布时间】:2014-06-06 11:28:28
【问题描述】:
我正在使用 WCF RESTful Web 服务,但出现此错误:
尝试反序列化参数时出错 http://tempuri.org/:aa。 InnerException 消息是“有一个 反序列化 WcfService1.Test 类型的对象时出错。约会时间 内容 '2014-05-31T18:30:00.000Z' 不以 '/Date(' 和 根据 JSON 的要求以 ')/' 结尾。'。请参阅 InnerException 更多细节。'。有关更多详细信息,请参阅服务器日志。异常堆栈 跟踪是:`
在 System.ServiceModel.Dispatcher.DataContractJsonSerializerOperationFormatter.DeserializeParameterPart(XmlDictionaryReader 阅读器,PartInfo 部分)在 System.ServiceModel.Dispatcher.DataContractJsonSerializerOperationFormatter.DeserializeParameter(XmlDictionaryReader 阅读器,PartInfo 部分)在 System.ServiceModel.Dispatcher.DataContractJsonSerializerOperationFormatter.DeserializeParameters(XmlDictionaryReader 阅读器,PartInfo[] 部分,Object[] 参数,PartInfo 返回信息, 对象和返回值)在 System.ServiceModel.Dispatcher.DataContractJsonSerializerOperationFormatter.DeserializeBodyCore(XmlDictionaryReader reader, Object[] 参数, Boolean isRequest) at
来自 jQuery 的输入:
$.ajax({
url: "Restful.svc/new/one",
type: "POST",
contentType: "application/json; charset=utf-8",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
data: JSON.stringify({ aa: { xaaaaaa: "2014-05-31T18:30:00.000Z" } }),
dataType: 'json',
processData: true,
success: function (msg) {
alert(msg.helloWorldResult);
},
error: function (msg) {
var y = 0;
}
});
WCF 服务:
[OperationContract]
[WebInvoke(Method = "POST",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "new/one")]
String helloWorld(Test aa);
测试类:
public class Test
{
[JsonProperty(ItemConverterType=typeof(IsoDateTimeConverter))]
public DateTime xaaaaaa { get; set; }
}
如果我将输入 xaaaaaa 传递为:/Date(new Date.valueOf().toString())/ 它会接受。如何更改 WCF 服务中的默认日期格式化程序以使用 IsoDateFormat 进行序列化和反序列化。
我已尝试修改路由表设置,但我无法找到大多数库。如果我使用 JSON.NET,它说它默认使用 ISO 格式。如何将其设置为在 WCF Web 服务中使用?
【问题讨论】:
-
我做了完全相同的事情,一旦序列化完成,我必须修剪引号 (")。这是因为 DataContract 序列化程序(它是 WCF 服务中的默认序列化程序)序列化最终结果并在其周围添加引号(例如:
"\"0001-01-01T00:00:00"\",当在客户端收到时)。谢谢@Irb,它现在可以工作了。
标签: c# wcf datetime serialization json-deserialization