当前位置:   article > 正文

Newtonsoft.Json设置忽略某些字段

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

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  [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);

  }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

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


    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118

返回结果:

{
	"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
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/137356
推荐阅读
相关标签
  

闽ICP备14008679号