一,前言
最近做项目采用Json形式和其他客户端交互,借助于Newtonsoft.Json 。
由于业务场景不同,输出的Json内容也不同。要想忽略的属性,可以借助Newtonsoft.Json的特性,在实体前面添加特性[JsonIgnore]即可,但有时候会根据业务需求,在不同的地方输出同一个实体中不同的属性,所以添加特性的方式显然不能满足要求。例如user表,在A场景下需要password;B场景下不需要。
二,解决办法
可以重写Newtonsoft.Json的DefaultContractResolver类。
步骤一:继承DefaultContractResolver
新建类继承Newtonsoft.Json的类 DefaultContractResolver,重写CreateProperties方法,代码如下:
public class JsonPropertyContractResolver : DefaultContractResolver { IEnumerable<string> lstInclude; public JsonPropertyContractResolver(IEnumerable<string> includeProperties) { lstInclude = includeProperties; } protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization) { return base.CreateProperties(type, memberSerialization).ToList().FindAll(p => lstInclude.Contains(p.PropertyName));//需要输出的属性 } } } }
步骤二:使用方法
假设我们需要转化为Json的实体列表是Product,场景一,这个列表中的Price属性不需要转成Json到前端;场景二,这个列表中的ShopID和Count属性不需要转成Json到前端。代码如下:
List<Product> ProductList = new List<Product> { new Product { ShopID = 1, Price=10, Count=4, Name = "商品一" }, new Product { ShopID = 2, Price=11, Count=3, Name = "商品二" }, new Product { ShopID = 1, Price=12, Count=1, Name = "商品三" }, new Product { ShopID = 2, Price=17, Count=10, Name = "商品四" }, new Product { ShopID = 3, Price=13, Count=2, Name = "商品五" } }; //场景一 string jsonString = JsonConvert.SerializeObject(ProductList, Formatting.Indented, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore, ContractResolver = new JsonPropertyContractResolver(new List<string> { "ShopID", "Name", "Count" }) }); Console.WriteLine("场景一:" + jsonString); //场景二 JsonSerializerSettings settings = new JsonSerializerSettings(); settings.Formatting = Formatting.Indented; settings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; settings.ContractResolver = new JsonPropertyContractResolver(new List<string> { "Name", "Price" }); Console.WriteLine("场景二:" + JsonConvert.SerializeObject(ProductList, settings));
输出结果: