国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

JavaScriptSerializer序列化成Json時(shí)DateTime類型數(shù)據(jù)的處理

2019-11-17 03:09:09
字體:
供稿:網(wǎng)友

javaScriptSerializer序列化成Json時(shí)DateTime類型數(shù)據(jù)的處理

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

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") });

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


發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 始兴县| 额尔古纳市| 都安| 文安县| 永康市| 景德镇市| 阿鲁科尔沁旗| 盈江县| 虞城县| 靖州| 武清区| 无为县| 绥阳县| 陵川县| 瑞金市| 东兰县| 深泽县| 元谋县| 美姑县| 多伦县| 武鸣县| 泗水县| 湖州市| 安乡县| 育儿| 北海市| 陆良县| 冀州市| 襄汾县| 绍兴市| 淮北市| 永新县| 正阳县| 恩平市| 江口县| 乾安县| 黔江区| 三台县| 体育| 伊宁县| 灵石县|