首页 > 其他分享 >使用Newtonsoft.Json在Net6中设置时间格式(含T/不含T)

使用Newtonsoft.Json在Net6中设置时间格式(含T/不含T)

时间:2023-04-03 17:33:22浏览次数:49  
标签:Newtonsoft string dateFormat DateTime Json options SerializerOptions Net6 public

实例一:       
JsonSerializerSettings jsonSettings = new JsonSerializerSettings { DateFormatString = "yyyy-MM-dd HH:mm:ss.fff", }; Model model = new Model { CreatedAt = DateTime.UtcNow }; string json = JsonConvert.SerializeObject(model, Formatting.None, jsonSettings); Console.WriteLine(json);
    public class Model
    {
        public DateTime CreatedAt { get; set; }
    }

实例二:在net6中自定义

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    // 配置 JsonSerializerOptions
    app.UseJson(options =>
    {
        options.SerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
        options.SerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault;
        options.SerializerOptions.WriteIndented = true;
        options.SerializerOptions.Converters.Add(new DateTimeConverter("yyyy-MM-ddTHH:mm:ssZ"));
    });

    // 其他配置与中间件
    // ...
}
public class DateTimeConverter : JsonConverter<DateTime>
{
    private readonly string _dateFormat;

    public DateTimeConverter(string dateFormat)
    {
        _dateFormat = dateFormat;
    }

    public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
    {
        if (reader.TokenType == JsonTokenType.String && DateTime.TryParseExact(reader.GetString(), _dateFormat, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind, out DateTime dateTime))
        {
            return dateTime;
        }

        return default;
    }

    public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
    {
        writer.WriteStringValue(value.ToString(_dateFormat));
    }
}

 

标签:Newtonsoft,string,dateFormat,DateTime,Json,options,SerializerOptions,Net6,public
From: https://www.cnblogs.com/tx1185498724/p/17283753.html

相关文章

  • fastjson 把json字符串转成对象
    Stringjson="[{\"fid\":0,\"id\":1,\"name\":\"fjk的测试类目一级\"},{\"fid\":1,\"id\":2,\"name\":\"fjk的测试类目二级\"},{\"fid\":88,\"id\":98,\&q......
  • .net6的IIS发布部署
    1.打开控制面板,打开程序2.点击启动或关闭windows功能 3.在其中选择要设置的IIS功能  4.重启IIS服务5.发布项目 6.在开始菜单搜索IIS,点击IIS管理器   7.右击网站,点击添加网站,进行网站配置  8.可以自定义网站名称(可以是项目名,方便查询),物理路径就......
  • .net6(.net core)使用MailKit收取邮件乱码的问题
    配置IMAP地址后,MailKit读取邮件时中文乱码。主要问题是.netcore以后默认缺失中文字符集。1.安装System.Text.Encoding.CodePages包  2.在Main方法注册staticvoidMain(){//注册字符集,缺失字符集,一些中文编码数据为乱码Encoding......
  • Jackson_json转换工具
                         ......
  • 要用好json,掌握好json的类型
    SON是用于Web上数据交换的最广泛使用的数据格式。它完全独立于语言。基于JavaScript编程语言的一个子集,易于理解和生成。那json的数据类型有哪些?接下来我们就来给大家讲解一下这方面的内容。JSON主要支持6种数据类型:字符串(String):JSON字符串必须用双引号编写,如C语言,JSON中有各......
  • JSON的创建格式和常用方法
       ......
  • .net6 制作elementplus离线文档
    1、新建net6项目设置配置信息<ProjectSdk="Microsoft.NET.Sdk.Web"><PropertyGroup><TargetFramework>net6.0</TargetFramework><Nullable>enable</Nullable><ImplicitUsings>enable</ImplicitUsings>......
  • site-packages/flask/json/init.py from future import annotations future feature a
    如果在使用Flask库时,出现了“futurefeatureannotationsisnotdefined”的错误,可能是因为Python解释器版本太低。在Python3.7及以下版本中,from__future__importannotations是不支持的,因此需要升级到Python3.8或更高版本。如果升级Python解释器版本不可行,可以......
  • 两种方式自定制基于JWT的认证类BaseAuthentication和BaseJSONWebTokenAuthentication
    1.基于BaseAuthentication的自定义方法  2.views中调用自定义方法MyJwtAuthentication验证  3.基于BaseAuthentication的自定义方法测试:token过期  4.基于BaseAuthentication的自定义方法测试:token数据有错误,需检查token正确性  5.基于BaseAuthenticati......
  • 通过Sysmon+Nxlogs收集Windows Server 2012服务器日志-并以Syslog形式发送Json格式数
    0x01环境介绍WindowsServer2012已经安装部署好了域控,目的除了收集Windows服务器本身的日志外还收集域控环境下的各种日志。0x02Nxlog配置和使用使用社区版本即可,下载地址:https://nxlog.co/downloads/nxlog-ce#nxlog-community-edition使用的版本是当前最新版本安装过程就省略,......