1. 添加System.Web.Extensions引用
2. 创建类
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace WindowsFormsApplication1 { public class Team { public string Name { get;set;} public List<User> Users { get; set; } } public class User { public int ID { get; set; } public string Name { get; set; } } }
3. 序列化和反序列化
List<User> Users1 = new List<User>(); for (int i = 0; i < 10; i++) { User s = new User { ID = i, Name = "XXX" }; Users1.Add(s); } JavaScriptSerializer js = new JavaScriptSerializer(); Team team = new Team() { Name = "SSSSSSSS", Users = Users1 }; ///Class --- > Json string json = js.Serialize(team); MessageBox.Show(json); ///JSon --- > Class JavaScriptSerializer jss = new JavaScriptSerializer(); Team team1 = js.Deserialize<Team>(json);