1 [WebMethod(Description = "将一个DataTable对象转化成JSON")] 2 public string GetJSON() 3 { 4 JavaScriptSerializer jss = new JavaScriptSerializer(); 5 DataTable table = new DataTable("wangzhen"); 6 table.Columns.Add("Name"); 7 table.Columns.Add("Sex"); 8 9 table.Rows.Add(new object[] { "张三", "male" }); 10 table.Rows.Add(new object[] { "李四", "male" }); 11 table.Rows.Add(new object[] { "王五", "male" }); 12 13 List<Person> list = new List<Person>(); 14 Person person = null; 15 for (int i = 0; i < table.Rows.Count; i++) 16 { 17 person = new Person(); 18 person.Name = table.Rows[i]["Name"].ToString(); 19 person.Sex = table.Rows[i]["Sex"].ToString(); 20 list.Add(person); 21 } 22 return jss.Serialize(list); 23 }