Json序列化,不序列化某个字段


在指定的可为空的字段上增加特性“[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]”即可;

 

  class Program
    {
        static void Main(string[] args)
        {
            TestMan test = new TestMan();
            test.Name = "Name";

            string end = JsonConvert.SerializeObject(test);
            Console.WriteLine(end);
            Console.ReadKey();
        }     
    } 
    public class TestMan
    {
        [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
        public string Name { get; set; }

        [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
        public int? Age { get; set; }
    }

  

 

执行效果如下(序列化的结果中,没有Age字段的相关信息):

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM