将Java日期序列化为.Net Json日期格式



编写Android应用程序以与现有的.Net API交互,并遇到日期序列化问题。我知道.Net Json的日期格式是/date(时间戳时区)/,但是即使我这样格式化它,我仍然会收到错误:'There was an error deserializing the object of type ShoutsOut.Entities.MobileUser. DateTime content '/Date(1469457720840-0000)/' does not start with '/Date(' and end with ')/' as required for JSON.'

全栈跟踪:

at System.Runtime.Serialization.XmlObjectSerializer.ReadObjectHandleExceptions(XmlReaderDelegator reader, Boolean verifyObjectName, DataContractResolver dataContractResolver) 
at System.Runtime.Serialization.Json.DataContractJsonSerializer.ReadObject(XmlDictionaryReader reader, Boolean verifyObjectName) 
at System.ServiceModel.Dispatcher.SingleBodyParameterDataContractMessageFormatter.ReadObject(Message message) 
at System.ServiceModel.Dispatcher.SingleBodyParameterMessageFormatter.DeserializeRequest(Message message, Object[] parameters) 
at System.ServiceModel.Dispatcher.DemultiplexingDispatchMessageFormatter.DeserializeRequest(Message message, Object[] parameters) 
at System.ServiceModel.Dispatcher.UriTemplateDispatchFormatter.DeserializeRequest(Message message, Object[] parameters) 
at System.ServiceModel.Dispatcher.DispatchOperationRuntime.DeserializeInputs(MessageRpc& rpc) 
at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc) 
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc) 
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage11(MessageRpc& rpc) at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet) 

我的格式是这样的:

public static String toJson(Date date) {
    return "\/Date(" + date.getTime() + "-0000)\/";
}

尝试:

  • 删除时区
  • 删除//
  • 在日期之间添加

发现格式略有不同的""\/Date("+date.getTime+"-0500)\/"",也不起作用

这个答案为我解决了这个问题,基本上必须将Joda时间库中的日期数据类型更改为DateTime,并实现了自定义序列化程序/反序列化程序

相关内容

  • 没有找到相关文章

最新更新