不是的JavaScriptSerializer反序列化的DateTime /时间跨度正确 [英] JavaScriptSerializer not deserializing DateTime/TimeSpan Properly
问题描述
有一个问题,日期时间/时间跨度似乎不能用的JavaScriptSerializer正确反序列化。
当我反序列化的时间跨度后,即可获取对象后面是空的,如果我使用的DateTime那么时间均走出低谷。
没有找到这篇文章,但它并不能真正帮助我太多。
http://www.west-wind.com/weblog/ShowPost.aspx ?ID = 471402
任何人有什么想法?我应该也许尝试了json.net库?
公共类JsonFilter:ActionFilterAttribute
{
公共字符串参数{搞定;组; }
公共类型JsonDataType {搞定;组; }
公共覆盖无效OnActionExecuting(ActionExecutingContext filterContext)
{
如果(filterContext.HttpContext.Request.ContentType.Contains(应用/ JSON))
{
串inputContent;
使用(VAR SR =新的StreamReader(filterContext.HttpContext.Request.InputStream))
{
inputContent = sr.ReadToEnd();
} 串行的JavaScriptSerializer =新的JavaScriptSerializer();
VAR的结果= serializer.Deserialize(inputContent,JsonDataType);
filterContext.ActionParameters [参数] =结果;
}
}
}公共类RosterItem
{
公共RosterItem()
{
评论=新的List< Form.Comment>();
}
公共布尔脏{搞定;组; }
公众诠释ID {搞定;组; }
公众诠释{STAFFID获得;组; }
公共字符串描述{搞定;组; }
公众诠释activityId {搞定;组; }
公众的DateTime日期{搞定;组; }
公共时间跨度的startTime {搞定;组; }
公共时间跨度ENDTIME {搞定;组; }
公开名单< Form.Comment>评论{搞定;组; }
} [JsonFilter(参数=rosterItem,JsonDataType = typeof运算(RosterItem))]
公众诠释SaveRosterEntry(RosterItem rosterItem)
{
RosterEntry rosterEntry = rosterEntryRepository.GetRosterEntry(rosterItem.id);
如果(rosterEntry == NULL)
{
rosterEntry =新RosterEntry();
rosterEntryRepository.Add(rosterEntry);
}
rosterEntry.ActivityID = rosterItem.activityId;
rosterEntry.StartTime = rosterItem.startTime;
rosterEntry.EndTime = rosterItem.endTime;
rosterEntry.RosterDate = rosterItem.date;
rosterEntry.RosterEmployeeID = rosterItem.staffId;
rosterEntryRepository.Save();
返回rosterEntry.RosterEntryID;
}
这将解决您的问题,如果你有同样的问题。
http://blog.devarchive.net/2008/ 02 /序列化,日期时间值,using.html
所有的DateTime对象需要显式地指定为UTC。
Having a problem where DateTime/TimeSpan doesn't seem to deserialize properly with JavaScriptSerializer. When I get the Object back after deserializing the TimeSpan is empty and if I use DateTime then the times are all out of whack. Did find this article but it didn't really help me too much. http://www.west-wind.com/weblog/ShowPost.aspx?id=471402
Anyone have any ideas? Should I maybe try the json.net library?
public class JsonFilter : ActionFilterAttribute
{
public string Param { get; set; }
public Type JsonDataType { get; set; }
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (filterContext.HttpContext.Request.ContentType.Contains("application/json"))
{
string inputContent;
using (var sr = new StreamReader(filterContext.HttpContext.Request.InputStream))
{
inputContent = sr.ReadToEnd();
}
JavaScriptSerializer serializer = new JavaScriptSerializer();
var result = serializer.Deserialize(inputContent, JsonDataType);
filterContext.ActionParameters[Param] = result;
}
}
}
public class RosterItem
{
public RosterItem()
{
comments = new List<Form.Comment>();
}
public Boolean dirty { get; set; }
public int id { get; set; }
public int staffId { get; set; }
public String description { get; set; }
public int activityId { get; set; }
public DateTime date { get; set; }
public TimeSpan startTime { get; set; }
public TimeSpan endTime { get; set; }
public List<Form.Comment> comments { get; set; }
}
[JsonFilter(Param = "rosterItem", JsonDataType = typeof(RosterItem))]
public int SaveRosterEntry(RosterItem rosterItem)
{
RosterEntry rosterEntry = rosterEntryRepository.GetRosterEntry(rosterItem.id);
if (rosterEntry == null)
{
rosterEntry = new RosterEntry();
rosterEntryRepository.Add(rosterEntry);
}
rosterEntry.ActivityID = rosterItem.activityId;
rosterEntry.StartTime = rosterItem.startTime;
rosterEntry.EndTime = rosterItem.endTime;
rosterEntry.RosterDate = rosterItem.date;
rosterEntry.RosterEmployeeID = rosterItem.staffId;
rosterEntryRepository.Save();
return rosterEntry.RosterEntryID;
}
This will fix your issue if you ever have the same problem.
http://blog.devarchive.net/2008/02/serializing-datetime-values-using.html
All DateTime objects need to be specified explicitly as UTC.
这篇关于不是的JavaScriptSerializer反序列化的DateTime /时间跨度正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!