用户实体:
1 public class UserModel 2 { 3 public long? UserId { get; set; } 4 public string UserName { get; set; } 5 public string UserPwd { get; set; } 6 public long? RoleId { get; set; } 7 public string RoleName { get; set; } 8 public int UserType { get; set; } 9 public int UserStatus { get; set; } 10 public DateTime? CreateTime { get; set; } 11 public DateTime? UpdateTime { get; set; } 12 public long? CreateUserId { get; set; } 13 public long? UpdateUserId { get; set; } 14 }
利用JSON.NET的JsonConvert对象序列化(用户实体)为json字符串:
UserModel model =new UserModel(); model.UserId = 0001; model.UserName = "张三"; model.UserPwd = "nibabadexiaobibi"; model.RoleId = 3; model.UserType = 2; model.UserStatus = 1; model.CreateTime = Datetime.Now; model.UpdateTime = DateTime.Now; model.CreateUserId = 88; model.CreateUserId = 89;
var jsonData = Newtonsoft.Json.JsonConvert.SerializeObject(model);
result:
{
"UserId":0001,
"UserName":"张三",
"UserPwd":"nibabadexiaobibi",
"RoleId":3,
"UserType":2,
"UserStatus":1,
"CreateTime":"2016-08-09 00:00:00",
"UpdateTime":"2016-08-09 00:00:00",
"CreateUserId":88,
"UpdateUserId":89
}
再将json字符串转换为实体对象:
UserModel model = Newtonsoft.Json.JsonConvert.DeserializeObject<UserModel>(jsonData);
output:model.UserName