欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

asp.net core 3.0 MVC JSON 全局配置

程序员文章站 2024-01-16 08:42:58
asp.net core 3.0 MVC JSON 全局配置 System.Text.Json(default) 1. startup配置代码如下: 2. "官方API" Newtonsoft.Json 1. Install Package Microsoft.AspNetCore.Mvc.Newt ......

asp.net core 3.0 mvc json 全局配置

  • system.text.json(default)
  1. startup配置代码如下:
using system.text.encodings.web;
using system.text.json;
using system.text.unicode;

public void configureservices(iservicecollection services)
{
     services.addjsonoptions(options =>
            {
                //格式化日期时间格式
                options.jsonserializeroptions.converters.add(new datetimejsonconverter());
                //数据格式首字母小写
                //options.jsonserializeroptions.propertynamingpolicy =jsonnamingpolicy.camelcase;
                //数据格式原样输出
                options.jsonserializeroptions.propertynamingpolicy = null;
                //取消unicode编码
                options.jsonserializeroptions.encoder = javascriptencoder.create(unicoderanges.all);
                //忽略空值
                options.jsonserializeroptions.ignorenullvalues = true;
                //允许额外符号
                options.jsonserializeroptions.allowtrailingcommas = true;
                //反序列化过程中属性名称是否使用不区分大小写的比较
                options.jsonserializeroptions.propertynamecaseinsensitive = false;
            });
}
using system;
using system.text.json;
using system.text.json.serialization;


public class datetimejsonconverter : jsonconverter<datetime>
    {
        public override datetime read(ref utf8jsonreader reader, type typetoconvert, jsonserializeroptions options)
        {
            if (reader.tokentype == jsontokentype.string)
            {
                if (datetime.tryparse(reader.getstring(), out datetime date))
                    return date;
            }
            return reader.getdatetime();
        }

        public override void write(utf8jsonwriter writer, datetime value, jsonserializeroptions options)
        {
            writer.writestringvalue(value.tostring("yyyy-mm-dd hh:mm:ss"));
        }
    }
  1. 官方api
  • newtonsoft.json
  1. install-package microsoft.aspnetcore.mvc.newtonsoftjson -version 3.1.0
  2. startup配置代码如下:
using newtonsoft.json;
using newtonsoft.json.serialization;
public void configureservices(iservicecollection services)
{
    services.addnewtonsoftjson(options =>
            {
                //设置时间格式
                options.serializersettings.dateformatstring = "yyyy-mm-dd hh:mm:ss";
                //忽略循环引用
                options.serializersettings.referenceloophandling = referenceloophandling.ignore;
                //数据格式首字母小写
                //options.serializersettings.contractresolver = new camelcasepropertynamescontractresolver();
                //数据格式按原样输出
                options.serializersettings.contractresolver = new defaultcontractresolver();
                //忽略空值
                options.serializersettings.nullvaluehandling = nullvaluehandling.ignore;
            });
}