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
本文介绍了如何使用Newtonsoft.Json库中的JsonSerializerSettings配置项来调整JSON字符串的序列化方式,特别是如何设置NullValueHandling和Formatting选项,以实现忽略空值字段及控制序列化输出格式。
1万+

被折叠的 条评论
为什么被折叠?



