1、首先引入 System.Web.Extensions.dll
2、写入命名空间 System.Web.Script.Serialization
3、实现序列化。
class Program { static void Main(string[] args) { WebClient Client = new WebClient(); Client.Encoding = ASCIIEncoding.UTF8;
// 从WebAPI 接收数据 string str = Client.DownloadString("http://localhost:8083/api/SearchData/Data"); JavaScriptSerializer serializer = new JavaScriptSerializer(); List<EmpModel> Data = serializer.Deserialize<List<EmpModel>>(str); foreach (var item in Data) { Console.WriteLine("姓名:{0},编号:{1},部门:{2},邮箱:{3},工作时间{4}", item.EmpName, item.EmpCode, item.DeptName,item.EmpEmail, item.WorkDate); } Console.Read(); } /* 使用JavaScriptSerializer序列化 JavaScriptSerializer serializer = new JavaScriptSerializer(); var jsstr = serializer.Serialize(EmpModel); 对Json.net生成的字符串进行反序列化 var jsperson2 = serializer.Deserialize<Person>(newtonstr); */ }