实例一:
using System; using System.Web.Script.Serialization; namespace JSonDemo { //实体类 public class Person { public int Id { get; set; } public string Name { get; set; } } public partial class Test : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { Button2.Enabled = false; } protected void Button1_Click(object sender, EventArgs e) { var person = new Person {Id = 1, Name = "王小王"}; var jsonSerializer = new JavaScriptSerializer(); //执行序列化 var personStr = jsonSerializer.Serialize(person); Label1.Text = personStr; Button2.Enabled = true; } protected void Button2_Click(object sender, EventArgs e) { //执行反序列化 var jsonSerializer = new JavaScriptSerializer(); var _person = jsonSerializer.Deserialize<Person>(Label1.Text); Label2.Text = "这个是反序列化的结果:"+_person.Id +","+ _person.Name; } } }