Newtonsoft.Json设置忽略某些字段

using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestProject1
{
    /// <summary>
    /// 输出json时,设置忽略哪些字段不要
    /// </summary>
    public class PropsContractResolver : DefaultContractResolver
    {
        /// <summary>
        /// 要忽略的字段集合,Key传入字段名称
        /// </summary>
        public Dictionary<string, bool> IgnoreProps { get; set; }

        public PropsContractResolver(Dictionary<string, bool>  ignoreProps)
        {
            IgnoreProps = ignoreProps;
        }

        protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
        {
            IList<JsonProperty> list = base.CreateProperties(type, memberSerialization);
            
            //只保留清单有列出的属性
            return list.Where(p =>
            {
                //去掉的字段
                //string[] ignoreProps = { "Password", "Encrypted_salt" };
                // if (p.PropertyName.Equals("Password"))
                //if (ignoreProps.Contains(p.PropertyName))
                //{
                //    //去掉字段
                //    return false;
                //}

                if (p.PropertyName!=null && IgnoreProps.ContainsKey(p.PropertyName))
                {
                    return false;//去掉字段
                }
                return true;//保留字段
            }).ToList();
        }
    }
}

  [TestMethod]
  public async Task TestMethod1()
  {
      //注册接口
      var builder = WebApplication.CreateBuilder();
      ServicesRegister.Register(builder);
      var app = builder.Build();         
      AppServicesHelpter.App = app;

      IAh_userBll userBll = AppServicesHelpter.GetServices<IAh_userBll>();
      Ah_user ah_User = await userBll.GetAsync(130042493512608);

      //忽略字段
      JsonSerializerSettings settings = new JsonSerializerSettings();
      var props = new Dictionary<string, bool>() ;
      props.Add("Password",false);
      props.Add("Encrypted_salt", false);
      props.Add("Extend1", false);
      props.Add("Extend2", false);
      props.Add("P", false);
      props.Add("PageSize", false);
      props.Add("Creator", false);
      props.Add("Create_time", false);    
      settings.ContractResolver = new PropsContractResolver(props);

      string json=Newtonsoft.Json.JsonConvert.SerializeObject(ah_User, settings);

  }

Ah_user

using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using WebProjectNet7.DataBaseEntity.BaseEntity;

namespace WebProjectNet7.DataBaseEntity.Entity
{
    /// <summary>
    /// 实体,用户表
    /// </summary>
    [Table("ah_user")]
    public partial class Ah_user : BaseModel
    {
        /// <summary>
        /// 用户名称
        /// </summary>
        public string? User_name { get; set; }

        /// <summary>
        /// 用户编号(员工编号)
        /// </summary>
        public string? User_no { get; set; }

        /// <summary>
        /// 性别 
        /// 1为男,2为女
        /// </summary>
        public byte? Sex { get; set; }

        /// <summary>
        /// 出生日期
        /// </summary>
        public string? Birth_date { get; set; }

        /// <summary>
        /// 电话
        /// </summary>
        public string? Tel { get; set; }

        /// <summary>
        /// 邮箱
        /// </summary>
        public string? Email { get; set; }

        /// <summary>
        /// 证件类型
        /// EnumIDType,枚举
        /// </summary>
        public byte? Id_type { get; set; }

        /// <summary>
        /// 证件号
        /// </summary>
        public string? Id_number { get; set; }

        /// <summary>
        /// 角色Id,多个,用,分割
        /// </summary>
        public string?  Role_id { get; set; }

        /// <summary>
        /// 角色名称
        /// </summary>
        public string? Role_name { get; set; }

        /// <summary>
        /// 部门Id
        /// </summary>
        public long? Organization_id { get; set; }

        /// <summary>
        /// 部门名称
        /// </summary>
        public string? Organization_name { get; set; }

        /// <summary>
        /// 账号
        /// </summary>
        public string? Account { get; set; }

        /// <summary>
        /// 密码
        /// </summary>
        public string? Password { get; set; }

        /// <summary>
        /// 密码加盐
        /// </summary>
        public string? Encrypted_salt { get; set; }

        /// <summary>
        /// 启用状态 1=启用,2=禁用
        /// </summary>
        public byte? Status { get; set; }

        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime? Create_time { get; set; }

        /// <summary>
        /// 创建人
        /// </summary>
        public long?  Creator { get; set; }

        /// <summary>
        /// 修改时间
        /// </summary>
        public DateTime? Update_time { get; set; }

        /// <summary>
        /// 修改人
        /// </summary>
        public long? Updator { get; set; }


    }
}

返回结果:

{
	"User_name": "武松",
	"User_no": "1001",
	"Sex": null,
	"Birth_date": "2024年01月03日",
	"Tel": "15874584685",
	"Email": "10717@qq.com",
	"Id_type": 1,
	"Id_number": "51053692484156685",
	"Role_id": "146759246945184",
	"Role_name": "吏部尚书",
	"Organization_id": 130041939348384,
	"Organization_name": "大理寺",
	"Account": "wusong",
	"Status": 1,
	"Update_time": "2024-02-19T17:54:41.847",
	"Updator": 130042493512608,
	"Id": 130042493512608
}

参考文章:
https://www.tnblog.net/aojiancc2/article/details/2828

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王焜棟琦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值