JSON.Net 自定义Json序列化时间格式
发布时间:2020-12-16 09:00:58 所属栏目:asp.Net 来源:网络整理
导读:JSON.Net 自定义Json序列化时间格式 Intro 和 JAVA 项目组对接,他们的接口返回的数据是一个json字符串,里面的时间有的是Unix时间戳,有的是string类型,有的还是空,默认序列化规则没办法反序列化为时间, 所以自定义了一个 Json 时间转换器,支持可空时间
JSON.Net 自定义Json序列化时间格式 Intro和 JAVA 项目组对接,他们的接口返回的数据是一个json字符串,里面的时间有的是Unix时间戳,有的是string类型,有的还是空,默认序列化规则没办法反序列化为时间, 所以自定义了一个 Json 时间转换器,支持可空时间类型、string、long(Unix时间戳毫秒) Show me the codepublic class CustomDateTimeConverter : JavaScriptDateTimeConverter { /// <summary> /// 重写JavaScriptDateTimeConverter ReadJson 方法 </summary> <param name="reader">The <see cref="T:Newtonsoft.Json.JsonReader"/> to read from.</param> <param name="objectType">Type of the object.<param name="existingValue">The existing property value of the JSON that is being converted.<param name="serializer">The calling serializer.<returns></returns> override object ReadJson(JsonReader reader,Type objectType,object existingValue,JsonSerializer serializer) { if (reader.Value == null) //兼容可空时间类型 { return null; } else { if (reader.TokenType == JsonToken.Date) { return reader.Value; } else JsonToken.String) { DateTime dt = DateTime.Parse(reader.Value.ToString()); dt; } { new DateTime(1970,1,1)">0,1)">0,DateTimeKind.Utc).AddMilliseconds(Convert.ToInt64(reader.Value)).ToLocalTime(); } } } } ? How To Usevar model = JsonConvert.DeserializeObject<ResponseModel>(res,1)">new CustomDateTimeConverter());
? End如果你有更好的实现方法,欢迎提出 欢迎随时联系我 weihanli@outlook.com (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc-4 – OAuth2和DotNetOpenAuth – 实现Google自
- Asp.Net Zero轻量级审核流设计
- asp.net – 使用EF Projection的RIA Services DomainServic
- ASP.NET – 如何从数据库有效地创建动态用户特定菜单
- asp.net-mvc – Spark VS 2010 intellisense
- asp.net-mvc – 在MVC ASP.NET中使用/显示RSS源的简单方法
- [译]ASP.NET Core 2.0 网址重定向的方法
- asp.net-mvc – Cakephp在asp.net mvc中烘焙等效
- asp.net-mvc – 应该在ASP.NET MVC应用程序中缓存发生在哪里
- ASP.NET知识点总结2
推荐文章
站长推荐
- 启用AllowSorting时,ASP.NET GridView标题行文本
- .net – 在PerfMon实例中选择正确的AppPool
- asp.net-mvc – Sitecore搜索方面和计算字段
- asp.net – 当我在gridview中进行排序时触发了Gr
- asp.net – 如何以编程方式从LDAP检索信息
- asp.net-mvc-3 – C#.NET MVC3 ModelState.IsVal
- asp.net – Cookiless Session是安全隐患吗?
- 如何在IIS 7上的经典ASP中启用大型文件的上传?
- asp.net – 是否可以根据用户角色隐藏/显示Kendo
- asp.net – jQuery无法在我的主机上运行?
热点阅读