usingNewtonsoft.Json;usingNewtonsoft.Json.Serialization;usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceTestProject1{/// <summary>/// 输出json时,设置忽略哪些字段不要/// </summary>publicclassPropsContractResolver:DefaultContractResolver{/// <summary>/// 要忽略的字段集合,Key传入字段名称/// </summary>publicDictionary<string,bool> IgnoreProps {get;set;}publicPropsContractResolver(Dictionary<string,bool> ignoreProps){
IgnoreProps = ignoreProps;}protectedoverrideIList<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)){returnfalse;//去掉字段}returntrue;//保留字段}).ToList();}}}
[TestMethod]publicasyncTaskTestMethod1(){//注册接口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 =newJsonSerializerSettings();var props =newDictionary<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 =newPropsContractResolver(props);string json=Newtonsoft.Json.JsonConvert.SerializeObject(ah_User, settings);}
Ah_user
usingSystem;usingSystem.ComponentModel.DataAnnotations;usingSystem.ComponentModel.DataAnnotations.Schema;usingWebProjectNet7.DataBaseEntity.BaseEntity;namespaceWebProjectNet7.DataBaseEntity.Entity{/// <summary>/// 实体,用户表/// </summary>[Table("ah_user")]publicpartialclassAh_user:BaseModel{/// <summary>/// 用户名称/// </summary>publicstring? User_name {get;set;}/// <summary>/// 用户编号(员工编号)/// </summary>publicstring? User_no {get;set;}/// <summary>/// 性别 /// 1为男,2为女/// </summary>publicbyte? Sex {get;set;}/// <summary>/// 出生日期/// </summary>publicstring? Birth_date {get;set;}/// <summary>/// 电话/// </summary>publicstring? Tel {get;set;}/// <summary>/// 邮箱/// </summary>publicstring? Email {get;set;}/// <summary>/// 证件类型/// EnumIDType,枚举/// </summary>publicbyte? Id_type {get;set;}/// <summary>/// 证件号/// </summary>publicstring? Id_number {get;set;}/// <summary>/// 角色Id,多个,用,分割/// </summary>publicstring? Role_id {get;set;}/// <summary>/// 角色名称/// </summary>publicstring? Role_name {get;set;}/// <summary>/// 部门Id/// </summary>publiclong? Organization_id {get;set;}/// <summary>/// 部门名称/// </summary>publicstring? Organization_name {get;set;}/// <summary>/// 账号/// </summary>publicstring? Account {get;set;}/// <summary>/// 密码/// </summary>publicstring? Password {get;set;}/// <summary>/// 密码加盐/// </summary>publicstring? Encrypted_salt {get;set;}/// <summary>/// 启用状态 1=启用,2=禁用/// </summary>publicbyte? Status {get;set;}/// <summary>/// 创建时间/// </summary>publicDateTime? Create_time {get;set;}/// <summary>/// 创建人/// </summary>publiclong? Creator {get;set;}/// <summary>/// 修改时间/// </summary>publicDateTime? Update_time {get;set;}/// <summary>/// 修改人/// </summary>publiclong? Updator {get;set;}}}
返回结果:
{"User_name":"武松",
"User_no":"1001",
"Sex": null,
"Birth_date":"2024年01月03日",
"Tel":"15874584685",
"Email":"[email protected]",
"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}
标签:
json
Newtonsoft.Json
本文转载自: https://blog.csdn.net/u011511086/article/details/136185980
版权归原作者 王焜棟琦 所有, 如有侵权,请联系我们删除。
版权归原作者 王焜棟琦 所有, 如有侵权,请联系我们删除。