2012-11-10 112 views
6

我有一个问题,从昨天开始打破我的头,不知道如何处理它。反序列化JSON日期无时区

  • 我在我的数据库,包含以下值日期字段:

enter image description here

  • 然后我的应用程序中获得的价值,发送到我的网页形式。(价值仍然相同:)多数民众赞成罚款!

enter image description here

  • 在客户端,我把休息与警觉地看到,从JSON将正在添加值一个javascript(该值仍然是相同的:)多数民众赞成罚款!

enter image description here

的警报在客户端的代码是这样的:

// transaction json model 
var jsonTransaction = @(Html.Raw(Json.Encode(this.Model.Transaction))); 
alert(new Date(parseInt(jsonTransaction.Date.substr(6)))); 
  • 现在,当我的值发送回服务器,这是我所得到的

enter image description here

  • 最后在JSON的反序列化之后,我的日期时间是错误的!而不是第7天它现在第8天???????

enter image description here

这是反序列化的代码:

public JsonResult SaveBill(string jsonTransaction, string jsonTranDetails) 
{ 
    _appState = this.AppState; 
    JsonResult returnVal = returnVal = Json(
     new { Success = true } 
    ); 

var transaction = JsonConvert.DeserializeObject<BillTransaction>(jsonTransaction, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore }); 

如何解决的日期这一问题的任何线索,我应该得到相同的日期,因为我没有任何改变。希望有人能指导我解决问题。

在此先感谢。

+0

如果您使用Newtonsoft的JSON.NET,则此问题消失。 ASP.NET MVC 4的新Web API使用相同的解析器。 –

+0

这应该讨论所有你需要知道的:http://www.hanselman.com/blog/OnTheNightmareThatIsJSONDatesPlusJSONNETAndASPNETWebAPI。ASPX –

+0

我可以得到相同的日期,唯一的方法是在服务器侧执行此操作: JsonConvert.DeserializeObject (jsonTransaction,新JsonSerializerSettings(){NullValueHandling = NullValueHandling.Ignore,DateFormatHandling = DateFormatHandling.MicrosoftDateFormat,DateTimeZoneHandling = DateTimeZoneHandling.Local }); 但不知道是否使用DateTimeZoneHandling.Local它好吗? – VAAA

回答

0

的Java脚本中使用通用时间,当它convering强前解析日期的currentdate =新的日期(123232) 所以当你发送日期的客户将其转换为ISO日期如 确保日期是UTC 。

return String.Format("{0:yyyy-MM-ddTHH:mm:ss.fffZ}", dt); 

BTY我已经创建的JSON转换为覆盖任何日期序列化客户端使用上述功能。