时间格式处理:
IsoDateTimeConverter timeConverter = new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" };
var obj = new { state = true, message = capitalDetailList };
Response.Write(JsonConvert.SerializeObject(obj, Formatting.Indented, timeConverter));
执行结果:
序列化的时候不序列化值为NULL的字段
处理前的数据:
{
"state": true,
"message": {
"Bid": 2,
"ForumID": 1,
"forumName": "各地车圈",
"Title": "碰一下倾家荡产",
"Content": null,
"Tags": null,
"LikeCount": 0,
"RepleyCount": 0,
"Uid": 353,
"CreateTime": "2016-11-16 10:47:39",
"ReplyUid": -1,
"ReplyName": null,
"LastReplyTime": "0001-01-01 00:00:00",
"LastEditUid": 0,
"LastEditName": null,
"LastEditTime": "0001-01-01 00:00:00",
"IP": null,
"Location": null,
"IsShow": 0,
"IsDelete": 0,
"IsPicture": 0,
"IsAdjunct": 0,
"IsHot": 0,
"IsRecommend": 0,
"IsStick": 0,
"IsEssence": 1,
"Source": 0,
"Remark": null,
"Reserve1": null,
"Reserve2": null,
"Reserve3": null,
"Reserve4": null,
"userImage": "http://192.168.0.177:8/upload/user/source/ua_1611011343189718398.jpg ",
"userName": "ant1756383 ",
"ShowImg": [
" http://192.168.0.177:8/upload/bbsImage/48f6b3f5a99aa.jpg ",
" http://192.168.0.177:8/upload/bbsImage/9527735_100118379000_2.jpg ",
" http://192.168.0.177:8/upload/bbsImage/2009820131534.jpg "
]
}
处理方法
JsonSerializerSettings jsetting = new JsonSerializerSettings();
//序列化时不序列化值为NULL的字段
jsetting.NullValueHandling = NullValueHandling.Ignore;
var obj = new { state = true, message = BbsModel };
Response.Write(JsonConvert.SerializeObject(obj,Formatting.Indented,jsetting));
处理后的数据:
{
"state": true,
"message": {
"Bid": 4,
"ForumID": 1,
"forumName": "各地车圈",
"Title": "碰一下倾家荡产",
"LikeCount": 0,
"RepleyCount": 0,
"Uid": 353,
"CreateTime": "2016-11-16T10:47:52.523Z",
"ReplyUid": -1,
"LastReplyTime": "0001-01-01T00:00:00",
"LastEditUid": 0,
"LastEditTime": "0001-01-01T00:00:00",
"IsShow": 0,
"IsDelete": 0,
"IsPicture": 0,
"IsAdjunct": 0,
"IsHot": 0,
"IsRecommend": 0,
"IsStick": 0,
"IsEssence": 1,
"Source": 0,
"userImage": "http://192.168.0.177:8/upload/user/source/ua_1611011343189718398.jpg ",
"userName": "ant1756383 ",
"ShowImg": [
]
}
}
序列化时忽略默认值
处理前的数据:
{
"state": true,
"message": {
"Bid": 4,
"ForumID": 1,
"forumName": "各地车圈",
"Title": "碰一下倾家荡产",
"LikeCount": 0,
"RepleyCount": 0,
"Uid": 353,
"CreateTime": "2016-11-16T10:47:52.523Z",
"ReplyUid": -1,
"LastReplyTime": "0001-01-01T00:00:00",
"LastEditUid": 0,
"LastEditTime": "0001-01-01T00:00:00",
"IsShow": 0,
"IsDelete": 0,
"IsPicture": 0,
"IsAdjunct": 0,
"IsHot": 0,
"IsRecommend": 0,
"IsStick": 0,
"IsEssence": 1,
"Source": 0,
"userImage": "http://192.168.0.177:8/upload/user/source/ua_1611011343189718398.jpg ",
"userName": "ant1756383 ",
"ShowImg": [
]
}
}
处理方法:
JsonSerializerSettings jsetting = new JsonSerializerSettings();
jsetting.DefaultValueHandling = DefaultValueHandling.Ignore; //序列化时忽略默认值
//jsetting.DefaultValueHandling = DefaultValueHandling.Include; //序列化是包含默认值
var obj = new { state = true, message = BbsModel };
Response.Write(JsonConvert.SerializeObject(obj,Formatting.Indented,jsetting));
处理后的数据:
{
"state": true,
"message": {
"Bid": 4,
"ForumID": 1,
"forumName": "各地车圈",
"Title": "碰一下倾家荡产",
"Uid": 353,
"CreateTime": "2016-11-16T10:47:52.523Z",
"IsEssence": 1,
"userImage": "http://192.168.0.177:8/upload/user/source/ua_1611011343189718398.jpg ",
"userName": "ant1756383 ",
"ShowImg": [
]
}
}
全局序列化:
代码如下:
public JsonSerializerSettings quanjuxuliehua()
{
Newtonsoft.Json.JsonSerializerSettings setting = new Newtonsoft.Json.JsonSerializerSettings();
JsonConvert.DefaultSettings = new Func<JsonSerializerSettings>(() =>
{
//日期类型默认格式化处理
setting.DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Local;
setting.DateFormatString = "yyyy-MM-dd HH:mm:ss";
//空值处理
setting.NullValueHandling = NullValueHandling.Ignore;
高级用法九中的Bool类型转换 设置
//setting.Converters.Add(new BoolConvert("是,否"));
return setting;
});
return setting;
}
调用方法:
var obj = new { state = true, message = BbsModel };
Response.Write(JsonConvert.SerializeObject(obj, Formatting.Indented, quanjuxuliehua()));