JSON.Net 自定义Json序列化时间格式
JSON.Net 自定义Json序列化时间格式
Intro
和 JAVA 项目组对接,他们的接口返回的数据是一个json字符串,里面的时间有的是Unix时间戳,有的是string类型,有的还是空,默认序列化规则没办法反序列化为时间, 所以自定义了一个 Json 时间转换器,支持可空时间类型、string、long(Unix时间戳毫秒)
Show me the code
public 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>
/// <param name="existingValue">The existing property value of the JSON that is being converted.</param>
/// <param name="serializer">The calling serializer.</param>
/// <returns></returns>
public 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 if (reader.TokenType == JsonToken.String)
{
DateTime dt = DateTime.Parse(reader.Value.ToString());
return dt;
}
else
{
return new DateTime(, , , , , , DateTimeKind.Utc).AddMilliseconds(Convert.ToInt64(reader.Value)).ToLocalTime();
}
}
}
}
How To Use
var model = JsonConvert.DeserializeObject<ResponseModel>(res,new CustomDateTimeConverter());
End
如果你有更好的实现方法,欢迎提出
欢迎随时联系我 weihanli@outlook.com
JSON.Net 自定义Json序列化时间格式的更多相关文章
- .NET 自定义Json序列化时间格式
.NET 自定义Json序列化时间格式 Intro 和 JAVA 项目组对接,他们的接口返回的数据是一个json字符串,里面的时间有的是Unix时间戳,有的是string类型,有的还是空,默认序列化规 ...
- SpringMVC返回Json,自定义Json中Date类型格式
http://www.cnblogs.com/jsczljh/p/3654636.html —————————————————————————————————————————————————————— ...
- mongodb json序列化时间格式
利用bson解决 type error 报错问题. # 序列化 from bson import json_util import json aa = json.dumps(anObject, def ...
- python解决json序列化时间格式
简单实例 import json from datetime import datetime from datetime import date info = { "name": ...
- System.Text.Json 自定义Converter实现时间转换
Newtonsoft.Json与System.Text.Json区别 在 Newtonsoft.Json中可以使用例如 .AddJsonOptions(options => { options. ...
- js 处理Json 时间带T 时间格式
对于后台传过来的json数据是带T时间格式的坑处理的一些做法总结 new Date(data[j].addtime).toISOString().replace(/T/g, ' ').replace( ...
- Newtonsoft.Json 序列化和反序列化 时间格式
From : http://www.cnblogs.com/litian/p/3870975.html 1.JSON序列化 string JsonStr= JsonConvert.SerializeO ...
- Newtonsoft.Json 序列化和反序列化 时间格式 [转]
1.JSON序列化 string JsonStr= JsonConvert.SerializeObject(Entity); eg: A a=new A(); a.Name="Elain ...
- [转]Newtonsoft.Json 序列化和反序列化 时间格式
本文转自:http://www.cnblogs.com/litian/p/3870975.html 1.JSON序列化 string JsonStr= JsonConvert.SerializeObj ...
随机推荐
- [Swift]LeetCode86. 分隔链表 | Partition List
Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...
- [Java]LeetCode116. 填充同一层的兄弟节点 | Populating Next Right Pointers in Each Node
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...
- [SQL]LeetCode175. 组合两个表 | Combine Two Tables
Table: Person +-------------+---------+ | Column Name | Type | +-------------+---------+ | PersonId ...
- [Swift]LeetCode384. 打乱数组 | Shuffle an Array
Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. int[] n ...
- [Swift]LeetCode661. 图片平滑器 | Image Smoother
Given a 2D integer matrix M representing the gray scale of an image, you need to design a smoother t ...
- 面试官:说说一条查询sql的执行流程和底层原理?
一条查询SQL执行流程图如下 序章 自我介绍 我是一条sql,就是一条长长的字符串,不要问我长什么样,因为我比较傲娇. 额~~不是我不说啊,因为细说起来,我可以细分为DML(Update.Insert ...
- web开发中获取的各种高度和宽度
前端开发中经常需要获取页面还有屏幕的高度和宽度进行计算,此文即介绍如何用 JavaScript 还有 jQuery 获取这些尺寸. 1.简介 一个页面显示在浏览器内,浏览器又放置在屏幕窗口内,所以由里 ...
- CentOS开发ASP.NET Core入门教程
作者:依乐祝 原文地址:https://www.cnblogs.com/yilezhu/p/9891346.html 因为之前一直没怎么玩过CentOS,大多数时间都是使用Win10进行开发,然后程序 ...
- Hystrix针对不可用服务的保护机制以及引入缓存
之前我写过一篇博文,通过案例了解Hystrix的各种基本使用方式,在这篇文章里,我们是通过Hystrix调用正常工作的服务,也就是说,Hytrix的保护机制并没有起作用,这里我们将在HystrixPr ...
- Unity GC 优化要点
参考:http://blog.csdn.net/znybn1/article/details/76464896 为啥要点?因为讲的重点. 游戏运行时来存储数据,当这些数据不再被使用时,存储这些数据的内 ...