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]LeetCode828. 独特字符串 | Unique Letter String
A character is unique in string S if it occurs exactly once in it. For example, in string S = " ...
- [Swift]LeetCode979. 在二叉树中分配硬币 | Distribute Coins in Binary Tree
Given the root of a binary tree with N nodes, each node in the tree has node.val coins, and there ar ...
- [Swift]LeetCode982. 按位与为零的三元组 | Triples with Bitwise AND Equal To Zero
Given an array of integers A, find the number of triples of indices (i, j, k) such that: 0 <= i & ...
- layui动态设置checkbox选中状态
今天在使用jquery动态设置layui的checkbox元素的选中状态时始终只能取消选中,却不能重新勾选,点击勾选则没有问题,代码如下 if (value == "true") ...
- 交叉编译 tcpdump
目录 1. 下载 tcpdump 2. 交叉编译 3. 相关说明 1. 下载 tcpdump 官网:http://www.tcpdump.org/ 2. 交叉编译 交叉编译libpcap: $ wge ...
- Vue.js 动画
transition 动画: 当插入或删除包含在 transition 组件中的元素时,Vue将会做以下处理: 1.自动嗅探目标元素是否应用了css过滤或动画,如果是,在恰当的时机添加/删除c ...
- Java Jvm运行机制原理
一:简介 在学习Java虚拟机之前,也就是Jvm之前,我想大家能够带着问题去学习,这样的话,大家学习起来也会比较有所获! 1.Java虚拟机(Jvm)是什么? 2.Java虚拟机是用来干什么的? 3. ...
- BBS论坛(九)
9.1.权限和角色模型定义 (1)cms/models class CMSPermission(object): ALL_PERMISSION = 0b11111111 # 1.访问者的权限 VISI ...
- 厉害—Ansible管理windows集群
最近公司新项目需要安装400+windows server 2012系统的工作站,想着怎么能像linux下运用ansible批量管理,linux就很简单了有ssh服务 但是下却没这么简单,但还是有办法 ...
- asp.net core系列 25 EF模型配置(隐藏属性)
一. 隐藏属性概述 隐藏属性也叫影子属性,该属性不是在.net实体类中定义的属性,而是在EFCore模型中为该实体类型定义的属性.这些属性的值和状态完全在变更跟踪器中维护.它有二个功能:(1)当数据库 ...