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

时间:2016-07-25 14:46:10

标签: java c# json date datetime

编写Android应用程序以与现有的.Net API进行交互并遇到日期序列化问题。我知道.Net Json日期格式是/ Date(timestamp-timezone)/,但即使我这样格式化,我仍然会收到错误:'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)\\/\"",无法正常工作

1 个答案:

答案 0 :(得分:0)

This answer为我解决了,基本上不得不从Joda时间库中将日期数据类型更改为DateTime,并实现了自定义序列化器/反序列化器