C#把json对象序列(List<T>)转化为字符串时去掉值为null的字段

listData为序列,如下所示:
var jsonSetting = new JsonSerializerSettings {NullValueHandling = NullValueHandling.Ignore};
var json = JsonConvert.SerializeObject(listData, Formatting.Indented, jsonSetting);

 如果不想生成的json字符串带有缩进格式符号\r\n则要使用如下方法:

 var jsonSetting = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore,Formatting=Formatting.None };
 insertInfo.GoodsDetailsStrJson = JsonConvert.SerializeObject(listData, jsonSetting);

 

posted @ 2023-03-13 09:19  James·wang  阅读(340)  评论(0编辑  收藏  举报