好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

c#中实体转换为json串去除空值

引用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);

这样就解决了!


查看更多关于c#中实体转换为json串去除空值的详细内容...

  阅读:116次