zoukankan      html  css  js  c++  java
  • asp.net core 3 WebApi System.Text.Json 返回数据配置

    一、System.Text.Json

    引用:

    using System.Text.Encodings.Web;
    using System.Text.Json;
    using System.Text.Unicode;
     
    

      

      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"));
            }
        }
    

      二、Newtonsoft.Json

    1. Install-Package Microsoft.AspNetCore.Mvc.NewtonsoftJson -Version 3.1.0
    2. startup配置代码如下: 
    3. 引用:
      using Newtonsoft.Json;
      using Newtonsoft.Json.Serialization;
      

        

      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;
                  });
       
  • 相关阅读:
    手机电阻式和电容式触摸屏九大区别
    AMBA、AHB、APB总线简介
    PHY芯片
    S3C2440A Memory design
    ARM、DSP、FPGA的技术特点和区别
    LPC2104的Boot与Remap详解(一)
    Memorymapped I/O
    Motherboard Chipsets and the Memory Map
    88f6282 notes
    太网设计FAQ:以太网MAC和PHY
  • 原文地址:https://www.cnblogs.com/wfpanskxin/p/12917941.html
Copyright © 2011-2022 走看看