zoukankan      html  css  js  c++  java
  • asp.net core 3.0 MVC JSON 全局配置

    原文链接:https://www.cnblogs.com/lenovo_tiger_love/archive/2019/12/17/12054253.html

    • System.Text.Json(default)
    1. startup配置代码如下:
     1 using System.Text.Encodings.Web;
     2 using System.Text.Json;
     3 using System.Text.Unicode;
     4 
     5 public void ConfigureServices(IServiceCollection services)
     6 {
     7      services.AddJsonOptions(options =>
     8             {
     9                 //格式化日期时间格式
    10                 options.JsonSerializerOptions.Converters.Add(new DatetimeJsonConverter());
    11                 //数据格式首字母小写
    12                 //options.JsonSerializerOptions.PropertyNamingPolicy =JsonNamingPolicy.CamelCase;
    13                 //数据格式原样输出
    14                 options.JsonSerializerOptions.PropertyNamingPolicy = null;
    15                 //取消Unicode编码
    16                 options.JsonSerializerOptions.Encoder = JavaScriptEncoder.Create(UnicodeRanges.All);
    17                 //忽略空值
    18                 options.JsonSerializerOptions.IgnoreNullValues = true;
    19                 //允许额外符号
    20                 options.JsonSerializerOptions.AllowTrailingCommas = true;
    21                 //反序列化过程中属性名称是否使用不区分大小写的比较
    22                 options.JsonSerializerOptions.PropertyNameCaseInsensitive = false;
    23             });
    24 }
     1 using System;
     2 using System.Text.Json;
     3 using System.Text.Json.Serialization;
     4 
     5 
     6 public class DatetimeJsonConverter : JsonConverter<DateTime>
     7     {
     8         public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
     9         {
    10             if (reader.TokenType == JsonTokenType.String)
    11             {
    12                 if (DateTime.TryParse(reader.GetString(), out DateTime date))
    13                     return date;
    14             }
    15             return reader.GetDateTime();
    16         }
    17 
    18         public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
    19         {
    20             writer.WriteStringValue(value.ToString("yyyy-MM-dd HH:mm:ss"));
    21         }
    22     }
    1. 官方API
    • Newtonsoft.Json
    1. Install-Package Microsoft.AspNetCore.Mvc.NewtonsoftJson -Version 3.1.0
    2. startup配置代码如下:
     1 using Newtonsoft.Json;
     2 using Newtonsoft.Json.Serialization;
     3 public void ConfigureServices(IServiceCollection services)
     4 {
     5     services.AddNewtonsoftJson(options =>
     6             {
     7                 //设置时间格式
     8                 options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
     9                 //忽略循环引用
    10                 options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
    11                 //数据格式首字母小写
    12                 //options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
    13                 //数据格式按原样输出
    14                 options.SerializerSettings.ContractResolver = new DefaultContractResolver();
    15                 //忽略空值
    16                 options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
    17             });
    18 }
    19  
  • 相关阅读:
    TWinHttp之二
    日志池
    TWinHTTP
    TJSONTableObject跨平台解析JSON
    TSQLTableJSON解析JSON
    TDocVariantData解析JSON
    Vue.js常用指令汇总(v-if//v-show//v-else//v-for//v-bind//v-on等)
    VUE -- 十分钟入门 Less
    VUE -- ejs模板的书写
    Go VUE --- vuejs在服务器部署?
  • 原文地址:https://www.cnblogs.com/yanglang/p/12054451.html
Copyright © 2011-2022 走看看