JSON序列化自動過濾NULL值
阿新 • • 發佈:2018-12-31
使用Newtonsoft.Json.dll 序列化為json時主動將NULL值過濾掉,具體做法:
var jSetting = new JsonSerializerSettings {NullValueHandling = NullValueHandling.Ignore}; var json = JsonConvert.SerializeObject(response, Formatting.Indented, jSetting);
之前轉換的JSON為:
{"header":{"responseCode":"200","responseDesc"":"執行成功","responseDescErrorMsg":"","responseDescTime":"2015-07-07T13:40:48.7162196+08:00"},"body":{"order":{"orderid":"S1507070001"},"product":null}}
參加程式碼後轉換出來的為:
{"header":{"responseCode":"200","responseDesc":"執行成功","responseDescErrorMsg":"","responseDescTime":"2015-07-07T13:40:48.7162196+08:00"},"body":{"order":{"orderid":"S1507070001"}}}