C# Newtonsoft.Json JsonSerializerSettings 全局序列化设置

  1. Newtonsoft.Json.JsonSerializerSettings setting = new Newtonsoft.Json.JsonSerializerSettings();
  2. JsonConvert.DefaultSettings = new Func(() =>
  3. {
  4.      //日期类型默认格式化处理
  5.   setting.DateFormatHandling = Newtonsoft.Json.DateFormatHandling.MicrosoftDateFormat;
  6. setting.DateFormatString = "yyyy-MM-dd HH:mm:ss";
  7.     //空值处理
  8. setting.NullValueHandling = NullValueHandling.Ignore;
  9. //高级用法九中的Bool类型转换 设置
  10. setting.Converters.Add( new BoolConvert( "是,否"));
  11. return setting;
  12.  

    });

     

    序列化时 指定Key的处理方式,为驼峰式  

    CamelCasePropertyNamesContractResolver

    //设置序列化时key为驼峰样式

    JsonSerializerSettings settings = new JsonSerializerSettings();

    settings.ContractResolver = new CamelCasePropertyNamesContractResolver();

    settings.Formatting = Formatting.Indented;

    string str = JsonConvert.SerializeObject(menus, settings);

     

     

你可能感兴趣的:(C# Newtonsoft.Json JsonSerializerSettings 全局序列化设置)