JSON输出时不输出某些属性值

转载

效果:

 #region MyRegion
            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, Newtonsoft.Json.Formatting.Indented, new JsonSerializerSettings
            {
                ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
                //ContractResolver = new JsonPropertyContractResolver(new List<string> { "ShopID", "Name", "Count" })
                ContractResolver = new JsonPropertyContractResolver(new List<string> {"Name", "Count" })
            });
            Console.WriteLine("场景一:" + jsonString);

            //场景二
            JsonSerializerSettings settings = new JsonSerializerSettings();
            settings.Formatting = Newtonsoft.Json.Formatting.Indented;
            settings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
            settings.ContractResolver = new JsonPropertyContractResolver(new List<string> { "Name", "Price" });
            Console.WriteLine("场景二:" + JsonConvert.SerializeObject(ProductList, settings));
            Console.ReadKey();
            #endregion
  public class Product
    {
        public int ShopID { get; set; }
        public int Price { get; set; }

        public int Count { get; set; }
        public string Name { get; set; }
    }

    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));//需要输出的属性  } }
        }
    }
原文地址:https://www.cnblogs.com/macT/p/12660869.html