gpt4 book ai didi

c# - DataContractJsonSerializerOperationFormatter 不反序列化使用 JSON.NET

转载 作者:太空狗 更新时间:2023-10-30 01:19:11 25 4
gpt4 key购买 nike

我正在使用 WCF RESTful 网络服务,但出现此错误:

There was an error while trying to deserialize parameter http://tempuri.org/:aa. The InnerException message was 'There was an error deserializing the object of type WcfService1.Test. DateTime content '2014-05-31T18:30:00.000Z' does not start with '/Date(' and end with ')/' as required for JSON.'. Please see InnerException for more details.'. See server logs for more details. The exception stack trace is:`

at System.ServiceModel.Dispatcher.DataContractJsonSerializerOperationFormatter.DeserializeParameterPart(XmlDictionaryReader reader, PartInfo part) at System.ServiceModel.Dispatcher.DataContractJsonSerializerOperationFormatter.DeserializeParameter(XmlDictionaryReader reader, PartInfo part) at System.ServiceModel.Dispatcher.DataContractJsonSerializerOperationFormatter.DeserializeParameters(XmlDictionaryReader reader, PartInfo[] parts, Object[] parameters, PartInfo returnInfo, Object& returnValue) at System.ServiceModel.Dispatcher.DataContractJsonSerializerOperationFormatter.DeserializeBodyCore(XmlDictionaryReader reader, Object[] parameters, 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 服务中使用它?

最佳答案

我正在使用 Newtonsoft 序列化日期格式以保存 ISODate 格式。我能够解决我的问题:

测试.cs:

[DataContract]
public class Test
{
public DateTime xaaaaaa { get; set; }

[DataMember(Name = "xaaaaaa")]
private string HiredForSerialization { get; set; }

[OnSerializing]
void OnSerializing(StreamingContext ctx)
{
this.HiredForSerialization = JsonConvert.SerializeObject(this.xaaaaaa).Replace('"',' ').Trim();
}

[OnDeserialized]
void OnDeserialized(StreamingContext ctx)
{
this.xaaaaaa = DateTime.Parse(this.HiredForSerialization);
}

}

jQuery:

$.ajax({
url: "Transfer.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) {
tester = msg.helloWorldResult; //"2014-06-01T00:00:00+05:30"
},
error: function (msg) {
var y = 0;
}
});

我从日期选择器 (jQuery) 中选择的日期:

Selected date : 1st-Jun-2014

WCF 服务看起来像这样:

    [OperationContract]
[WebInvoke(Method = "POST",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "new/one")]
public Test helloWorld(Test aa)
{
return aa;
}

效果很好!

关于c# - DataContractJsonSerializerOperationFormatter 不反序列化使用 JSON.NET,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24080764/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com