用戶實體:json
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字符串:spa
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字符串轉換爲實體對象:code
UserModel model = Newtonsoft.Json.JsonConvert.DeserializeObject<UserModel>(jsonData);output:model.UserName