麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > JavaScript > 正文

JavaScriptSerializer序列化成Json時DateTime類型數據的處理

2019-11-17 03:09:09
字體:
來源:轉載
供稿:網友

javaScriptSerializer序列化成Json時DateTime類型數據的處理

JavascriptSerializer在序列化時會將DateTime的數據序列化成類似//Date(626543800000)//這樣的值,找了很多方法都不如意,最后在一個博客找到了完美的解決方法,地址:http://blog.calyptus.eu/seb/2011/12/custom-datetime-json-serialization/,通過自定義類型轉換器的方式轉換成想要的格式,轉換器代碼如下:

public class DateTimeConverter : JavaScriptConverter{PRivate string format = "yyyy-MM-dd";public DateTimeConverter(string format){this.format = format;}public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer){return new JavaScriptSerializer().ConvertToType(dictionary, type);}public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer){if (!(obj is DateTime)) return null;return new CustomString(((DateTime)obj).ToString(format));}public override IEnumerable<Type> SupportedTypes{get { return new[] { typeof(DateTime) }; }}private class CustomString : Uri, IDictionary<string, object>{public CustomString(string str): base(str, UriKind.Relative){}void IDictionary<string, object>.Add(string key, object value){throw new NotImplementedException();}bool IDictionary<string, object>.ContainsKey(string key){throw new NotImplementedException();}ICollection<string> IDictionary<string, object>.Keys{get { throw new NotImplementedException(); }}bool IDictionary<string, object>.Remove(string key){throw new NotImplementedException();}bool IDictionary<string, object>.TryGetValue(string key, out object value){throw new NotImplementedException();}ICollection<object> IDictionary<string, object>.Values{get { throw new NotImplementedException(); }}object IDictionary<string, object>.this[string key]{get{throw new NotImplementedException();}set{throw new NotImplementedException();}}void ICollection<KeyValuePair<string, object>>.Add(KeyValuePair<string, object> item){throw new NotImplementedException();}void ICollection<KeyValuePair<string, object>>.Clear(){throw new NotImplementedException();}bool ICollection<KeyValuePair<string, object>>.Contains(KeyValuePair<string, object> item){throw new NotImplementedException();}void ICollection<KeyValuePair<string, object>>.CopyTo(KeyValuePair<string, object>[] array, int arrayIndex){throw new NotImplementedException();}int ICollection<KeyValuePair<string, object>>.Count{get { throw new NotImplementedException(); }}bool ICollection<KeyValuePair<string, object>>.IsReadOnly{get { throw new NotImplementedException(); }}bool ICollection<KeyValuePair<string, object>>.Remove(KeyValuePair<string, object> item){throw new NotImplementedException();}IEnumerator<KeyValuePair<string, object>> IEnumerable<KeyValuePair<string, object>>.GetEnumerator(){throw new NotImplementedException();}IEnumerator IEnumerable.GetEnumerator(){throw new NotImplementedException();}}}

然后注冊一下就行了:

public JavaScriptSerializer serializer = new JavaScriptSerializer();serializer.RegisterConverters(new JavaScriptConverter[] { new DateTimeConverter("yyyyMMdd") });

這樣就可以通過構造函數自定義serializer序列化的對象中包含DateTime類型的值的格式


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 九一成人 | av在线久草| 性欧美xxxx极品摘花 | 国产一区二区三区在线视频 | 久久久久免费电影 | 久久久中文| 欧美18—19sex性hd按摩 | 在线成人www免费观看视频 | 久久羞羞| 久久亚洲国产精品 | 黑人操穴 | 国产成人午夜精品 | 亚洲第一综合 | www.guochanav.com | 鲁丝一区二区二区四区 | 哪里可以看免费的av | 久久久久久免费 | 蜜桃网在线观看 | 日韩视频一区二区 | 精品国产91久久久 | 欧美成人黄色片 | 黄在线观看 | 国产一区二区三区精品在线观看 | 最近国产中文字幕 | 毛片在哪看| 羞羞的视频在线 | 91,视频免费看 | 九九热在线免费观看视频 | 最近中文字幕一区二区 | 欧美性生活免费视频 | 黄色大片www | 免费看日产一区二区三区 | 麻豆视频免费网站 | 性爱视频在线免费 | 国产一区二区视频网站 | 中文字幕在线视频日本 | 国产999精品久久久久 | 成人在线观看一区二区三区 | 视频一区二区国产 | 精品一区二区三区在线观看视频 | 九九热视频这里只有精品 |