版权声明:本文为CSDN博主「LucPromise」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/LucPromise/article/details/90035648
————————————————
引用Newtonsoft.Json类
使用string str=JsonConvert.SerializeObject(实体类)转换为字符串时,若实体类中有些字段为空值,那么转换后的Json串回出现空值的情况
比如:
//此处是截取的Json片段
{ "content": { "checkForm": { "aircraftNo": null, "billNo": null, "checkArea": null, "checkCustomsCode": "3104", "checkEndTime": null, "checkId": "CHECK_ID_20190423164506_VH3i65", "checkNum": "0", "checkPersonContactNumber": null, "checkPersonDept": null, "checkPersonName": null, "checkProcIdea": null, "checkProcResult": null, "checkRate": null, "checkRequestTotal": null, "checkResultTotal": null, "checkStartTime": null } } }
那么怎么去掉这样的Null值呢?
使用 JsonSerializerSettings 全局序列化设置
定义: JsonSerializerSettings jsonSetting = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }; 或者 setting.DateFormatHandling = Newtonsoft.Json.DateFormatHandling.MicrosoftDateFormat; //空值处理 setting.NullValueHandling = NullValueHandling.Ignore; 使用: string messageOut = JsonConvert.SerializeObject(实体类, Formatting.Indented,jsonSetting);
这样就解决了!