Newtonsoft.Json 转Json字符串为空不序列化

时间:2024-12-26 13:07:56

原文:Newtonsoft.Json 转Json字符串为空不序列化

版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。

Newtonsoft.Json 转Json字符串为空不序列化

static JsonSerializerSettings mJsonSettings = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore };
public static string ObjectToJson(object obj)
{
return JsonConvert.SerializeObject(obj, Formatting.Indented, mJsonSettings);
}

如果想返回的json 没有  \r\n

改成:

 static JsonSerializerSettings mJsonSettings = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore };
public static string ObjectToJson(object obj)
{
return JsonConvert.SerializeObject(obj, Formatting.None, mJsonSettings);
}

就是:

 Formatting.Indented

改为:

Formatting.None