记录Newtonsoft在序列成Json时的两个妙用

 

JsonSerializerSettings js = new JsonSerializerSettings();

//1、属性为Null时不做序列化输出

            js.NullValueHandling = NullValueHandling.Ignore;

 

//2、属性为DateTime时,做指定的格式输出

            js.Converters.Add(new IsoDateTimeConverter()
            {
                DateTimeFormat ="yyyy-MM-dd HH:mm:ss"
            });

 

JsonConvert.SerializeObject(obj, Formatting.None, js);

 

 //3、设定某个属性不做json序列化输出(在属性头加[JsonIgnore])

      [JsonIgnore]
       public string Srl
       {
           get;
           set;
       }

 

 

 

相关文章:

  • 2022-12-23
  • 2021-07-29
  • 2022-02-21
  • 2021-09-06
  • 2022-02-25
  • 2021-12-27
  • 2021-10-21
猜你喜欢
  • 2022-12-23
  • 2021-06-28
  • 2021-11-21
  • 2021-09-03
  • 2021-05-26
相关资源
相似解决方案