欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 文旅 > 旅游 > newtonsoft.json日期转化器

newtonsoft.json日期转化器

2025/2/24 0:52:50 来源:https://blog.csdn.net/qq_36437991/article/details/144875955  浏览:    关键词:newtonsoft.json日期转化器

格式化日期

DateTimeJsonConvert.cs

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace NewtonsoftStudy01
{/// <summary>/// 日期转化器/// </summary>public class DateTimeJsonConvert : JsonConverter{private readonly string _dateTimeFormat;/// <summary>/// 构造函数注入/// </summary>/// <param name="dateTimeFormat"></param>public DateTimeJsonConvert(string dateTimeFormat){_dateTimeFormat = dateTimeFormat;}/// <summary>/// 写入json/// </summary>/// <param name="writer"></param>/// <param name="value"></param>/// <param name="serializer"></param>public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer){if (value == null){writer.WriteNull();return;}var isDateTime = value.GetType().IsAssignableFrom(typeof(DateTime));if (!isDateTime){writer.WriteNull();return;}DateTime dateTime = (DateTime)value;writer.WriteValue(dateTime.ToString(_dateTimeFormat));}/// <summary>/// 读取json/// </summary>/// <param name="reader"></param>/// <param name="objectType"></param>/// <param name="existingValue"></param>/// <param name="serializer"></param>/// <returns></returns>public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer){if (reader.Value == null){return null;}DateTime? res = null;var str = reader.Value.ToString();if(DateTime.TryParse(str, out DateTime result)){res = result;}return res;}/// <summary>/// 是否能转化/// </summary>/// <param name="objectType"></param>/// <returns></returns>public override bool CanConvert(Type objectType){return typeof(DateTime).IsAssignableFrom(objectType);}}
}

调用

using Newtonsoft.Json;
using System;namespace NewtonsoftStudy01
{public class Test01{/// <summary>/// 唯一标识/// </summary>public int Id { get; set; }/// <summary>/// 评论时间/// </summary>[JsonConverter(typeof(DateTimeJsonConvert), "yyyy-MM-dd")]public DateTime? CreateTime { get; set; }}
}

结果

using Newtonsoft.Json;
using System;namespace NewtonsoftStudy01
{internal class Program{static void Main(string[] args){Console.WriteLine("序列化");var t1 = new Test01();t1.Id = 1;t1.CreateTime = DateTime.Now;var t1Json = JsonConvert.SerializeObject(t1);Console.WriteLine(t1Json);var t2 = new Test01();t2.Id = 2;t2.CreateTime = null;var t2Json = JsonConvert.SerializeObject(t2);Console.WriteLine(t2Json);Console.WriteLine("反序列化");var t1New = JsonConvert.DeserializeObject<Test01>(t1Json);Console.WriteLine(t1.CreateTime);var t2New = JsonConvert.DeserializeObject<Test01>(t2Json);Console.WriteLine(t2New.CreateTime);Console.ReadKey();}}
}

在这里插入图片描述

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词