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);
微信号:jamesworkshop 学习QQ群:364976091