zoukankan      html  css  js  c++  java
  • WebApi输出json 不要把首字母转为小写

                services.AddControllers().AddJsonOptions(c => {
                    c.JsonSerializerOptions.PropertyNamingPolicy = new NormalPolicy();
                });
        public class NormalPolicy : JsonNamingPolicy
        {
            public override string ConvertName(string name) => name;
        }

    这是webapi内置的json转换器,但有个问题,如果一个json对象,里面有个属性是int?类型,用字符串转换过去,会失败。

    {
      "age":"18"  
    }
    转对象
    class Person
    {
      public int? age {get;set;}  
    }
    会提示类型转换错误

    方法二(推举)

    nuget引用 Microsoft.AspNetCore.Mvc.NewtonsoftJson

    services.AddControllers().AddNewtonsoftJson(opt => {
     //忽略循环引用
     opt.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;

    //忽略null值
    opt.SerializerSettings.NullValueHandling =  Newtonsoft.Json.NullValueHandling.Ignore;
    opt.SerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.DefaultContractResolver(); });
  • 相关阅读:
    Prime Ring Problem 素数环
    下沙的沙子有几粒?
    小兔的棋盘
    超级楼梯
    一只小蜜蜂...
    变形课
    Buy the Ticket
    How Many Trees?
    通过拦截器来统计每个action的执行时间
    apache+tomcat+session(JK实现方式)
  • 原文地址:https://www.cnblogs.com/IWings/p/13528078.html
Copyright © 2011-2022 走看看