zoukankan      html  css  js  c++  java
  • 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));//需要输出的属性  } }
            }
        }
  • 相关阅读:
    杭电ACM 2052 Picture
    杭电ACM求平均成绩
    杭电ACM水仙花数
    cigarettes
    分数加减法
    推荐几个sql server牛人的博客
    npm 介绍
    centos Flash Player插件的安装
    node.js学习(1)
    查询功能:yum [list|info|search|provides|whatprovides] 参数
  • 原文地址:https://www.cnblogs.com/macT/p/12660869.html
Copyright © 2011-2022 走看看