1,引用
插件:Newtonsoft.Json.dll
命名空间:using Newtonsoft.Json;
2,json序列化
static void Serialize() { List<User> uList = new List<User>(); uList.Add(new User { Name = "xihu", Age = 20, Sex = false }); uList.Add(new User { Name = "qiyo", Age = 21, Sex = true }); uList.Add(new User { Name = "mokg", Age = 22, Sex = false }); //将user集合序列化 string s = JsonConvert.SerializeObject(uList); Console.WriteLine(s); }
3,反序列化
//反序列化
static void Deserialize(string jsonStr)
{ List<User> uList = JsonConvert.DeserializeObject<List<User>>(jsonStr); foreach(var u in uList) {
Console.WriteLine($"姓名:{u.Name},年龄:{u.Age},性别:{u.Sex}"); } }
不知道你们发现了没有,转换后性别的首字母变成了大写,原因吧,我暂时还不知道,睡觉去了,哈哈