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序列化时间格式的更多相关文章

  1. .NET 自定义Json序列化时间格式

    .NET 自定义Json序列化时间格式 Intro 和 JAVA 项目组对接,他们的接口返回的数据是一个json字符串,里面的时间有的是Unix时间戳,有的是string类型,有的还是空,默认序列化规 ...

  2. SpringMVC返回Json,自定义Json中Date类型格式

    http://www.cnblogs.com/jsczljh/p/3654636.html —————————————————————————————————————————————————————— ...

  3. mongodb json序列化时间格式

    利用bson解决 type error 报错问题. # 序列化 from bson import json_util import json aa = json.dumps(anObject, def ...

  4. python解决json序列化时间格式

    简单实例 import json from datetime import datetime from datetime import date info = { "name": ...

  5. System.Text.Json 自定义Converter实现时间转换

    Newtonsoft.Json与System.Text.Json区别 在 Newtonsoft.Json中可以使用例如 .AddJsonOptions(options => { options. ...

  6. js 处理Json 时间带T 时间格式

    对于后台传过来的json数据是带T时间格式的坑处理的一些做法总结 new Date(data[j].addtime).toISOString().replace(/T/g, ' ').replace( ...

  7. Newtonsoft.Json 序列化和反序列化 时间格式

    From : http://www.cnblogs.com/litian/p/3870975.html 1.JSON序列化 string JsonStr= JsonConvert.SerializeO ...

  8. Newtonsoft.Json 序列化和反序列化 时间格式 [转]

    1.JSON序列化 string JsonStr= JsonConvert.SerializeObject(Entity); eg:   A a=new A(); a.Name="Elain ...

  9. [转]Newtonsoft.Json 序列化和反序列化 时间格式

    本文转自:http://www.cnblogs.com/litian/p/3870975.html 1.JSON序列化 string JsonStr= JsonConvert.SerializeObj ...

随机推荐

  1. SUSE12Sp3-MongoDB安装

    1.解压 sudo mkdir /usr/local/mongodb # 创建mongodb目录 将mongodb-linux-x86_64-suse12-4.0.6.tgz复制到/usr/local ...

  2. Vue(day1)

    一.起步 <!-- 开发环境版本,包含了有帮助的命令行警告 --> <script src="https://cdn.jsdelivr.net/npm/vue/dist/v ...

  3. [Swift]LeetCode220. 存在重复元素 III | Contains Duplicate III

    Given an array of integers, find out whether there are two distinct indices i and j in the array suc ...

  4. [Swift]LeetCode501. 二叉搜索树中的众数 | Find Mode in Binary Search Tree

    Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred ...

  5. [Swift]LeetCode816. 模糊坐标 | Ambiguous Coordinates

    We had some 2-dimensional coordinates, like "(1, 3)" or "(2, 0.5)".  Then, we re ...

  6. [Swift]LeetCode964. 表示数字的最少运算符 | Least Operators to Express Number

    Given a single positive integer x, we will write an expression of the form x (op1) x (op2) x (op3) x ...

  7. .NET Core 使用 HttpClient SSL 请求出错的解决办法

    问题 使用 HTTP Client 请求 HTTPS 的 API 时出现 The certificate cannot be verified up to a trusted certificatio ...

  8. 【Vue】IView之table组件化学习(二)

    最基本的绑定table是这样的,需要columns和data两个属性. <template> <Card> <h4>表格栗子</h4> <Tabl ...

  9. 【Android】打开本地的html文件

    网上好多说法 但实际上说到点上的没有 不想写太长 直接进入正题 Intent intent = new Intent(Intent.ACTION_VIEW); intent.addCategory(I ...

  10. 技术分享:RxJS实战练习-经典游戏Breakout

    效果图 数据流分析 1.ticker$ 数据流 interval配合scheduler/animationFrame 作为游戏随时间变化的控制数据流 ticker$ = interval(this.T ...