JSON实例

实例一:

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;

        }
    }

}

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。