asp.net-mvc-2 – JavaScriptSerializer没有正确反序列化DateTime / TimeSpan

前端之家收集整理的这篇文章主要介绍了asp.net-mvc-2 – JavaScriptSerializer没有正确反序列化DateTime / TimeSpan前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
有一个问题,其中DateTime / TimeSpan似乎没有使用JavaScriptSerializer正确反序列化.
当我在反序列化后得到Object时,TimeSpan为空,如果我使用DateTime,那么时间就完全没了.
找到了这篇文章,但它并没有真正帮助我太多.
http://www.west-wind.com/weblog/ShowPost.aspx?id=471402

有人有主意吗?我应该尝试json.net库吗?

  1. public class JsonFilter : ActionFilterAttribute
  2. {
  3. public string Param { get; set; }
  4. public Type JsonDataType { get; set; }
  5. public override void OnActionExecuting(ActionExecutingContext filterContext)
  6. {
  7. if (filterContext.HttpContext.Request.ContentType.Contains("application/json"))
  8. {
  9. string inputContent;
  10. using (var sr = new StreamReader(filterContext.HttpContext.Request.InputStream))
  11. {
  12. inputContent = sr.ReadToEnd();
  13. }
  14.  
  15. JavaScriptSerializer serializer = new JavaScriptSerializer();
  16. var result = serializer.Deserialize(inputContent,JsonDataType);
  17. filterContext.ActionParameters[Param] = result;
  18. }
  19. }
  20. }
  21.  
  22. public class RosterItem
  23. {
  24. public RosterItem()
  25. {
  26. comments = new List<Form.Comment>();
  27. }
  28. public Boolean dirty { get; set; }
  29. public int id { get; set; }
  30. public int staffId { get; set; }
  31. public String description { get; set; }
  32. public int activityId { get; set; }
  33. public DateTime date { get; set; }
  34. public TimeSpan startTime { get; set; }
  35. public TimeSpan endTime { get; set; }
  36. public List<Form.Comment> comments { get; set; }
  37. }
  38.  
  39. [JsonFilter(Param = "rosterItem",JsonDataType = typeof(RosterItem))]
  40. public int SaveRosterEntry(RosterItem rosterItem)
  41. {
  42. RosterEntry rosterEntry = rosterEntryRepository.GetRosterEntry(rosterItem.id);
  43. if (rosterEntry == null)
  44. {
  45. rosterEntry = new RosterEntry();
  46. rosterEntryRepository.Add(rosterEntry);
  47. }
  48. rosterEntry.ActivityID = rosterItem.activityId;
  49. rosterEntry.StartTime = rosterItem.startTime;
  50. rosterEntry.EndTime = rosterItem.endTime;
  51. rosterEntry.RosterDate = rosterItem.date;
  52. rosterEntry.RosterEmployeeID = rosterItem.staffId;
  53. rosterEntryRepository.Save();
  54. return rosterEntry.RosterEntryID;
  55. }

解决方法

我在GitHub上的帖子中找到了答案:

https://github.com/NancyFx/Nancy/issues/336

基本上答案是创建一个新的TimeSpanJsonConverter,它继承自JavaScriptConverter,然后将其传递给序列化程序类的实例:

  1. var serializer = new System.Web.Script.Serialization.JavaScriptSerializer()
  2. serializer.RegisterConverters(new[] { new TimeSpanJsonConverter() });

全班供参考(由GrumpyDev编写):

  1. public class TimeSpanJsonConverter : JavaScriptConverter
  2. {
  3. public override IEnumerable<Type> SupportedTypes
  4. {
  5. get
  6. {
  7. return new[] { typeof(TimeSpan) };
  8. }
  9. }
  10.  
  11. public override object Deserialize(IDictionary<string,object> dictionary,Type type,JavaScriptSerializer serializer)
  12. {
  13. return new TimeSpan(
  14. this.GetValue(dictionary,"days"),this.GetValue(dictionary,"hours"),"minutes"),"seconds"),"milliseconds"));
  15. }
  16.  
  17. public override IDictionary<string,object> Serialize(object obj,JavaScriptSerializer serializer)
  18. {
  19. var timeSpan = (TimeSpan)obj;
  20.  
  21. var result = new Dictionary<string,object>
  22. {
  23. { "days",timeSpan.Days },{ "hours",timeSpan.Hours },{ "minutes",timeSpan.Minutes },{ "seconds",timeSpan.Seconds },{ "milliseconds",timeSpan.Milliseconds }
  24. };
  25.  
  26. return result;
  27. }
  28.  
  29. private int GetValue(IDictionary<string,string key)
  30. {
  31. const int DefaultValue = 0;
  32.  
  33. object value;
  34. if (!dictionary.TryGetValue(key,out value))
  35. {
  36. return DefaultValue;
  37. }
  38.  
  39. if (value is int)
  40. {
  41. return (int)value;
  42. }
  43.  
  44. var valueString = value as string;
  45. if (valueString == null)
  46. {
  47. return DefaultValue;
  48. }
  49.  
  50. int returnValue;
  51. return !int.TryParse(valueString,out returnValue) ? DefaultValue : returnValue;
  52. }
  53. }

猜你在找的asp.Net相关文章