实体类转json,空值字段不显示
实体类转json,不显示空值字段:
代码:
/// <summary> /// 转Json字符串,删除空值字段 /// </summary> /// <param name="obj"></param> /// <returns></returns> public static string ToJsonDelNull(this object obj) { var jsonSetting = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }; return JsonConvert.SerializeObject(obj, Newtonsoft.Json.Formatting.Indented, jsonSetting); }