ASP.Net json与对象互转

版权声明:转载原创文章请以超链接形式请注明原文章出处,尊重作者,尊重原创!


恰饭广告




实现效果:

实现效果

注意:项目安装json转换工具

安装json转换工具

两个学生类:

Student.cs代码

    public class Student
    {
        public string stuNO { get; set; }
        public string stuName { get; set; }
        public Student(string stuNO, string stuName)
        {
            this.stuNO = stuNO;
            this.stuName = stuName;
        }
    }

StuList.cs代码

    public class StuList
    {
        public int count { get; set; }
        public List<Student> data;
    }

WebForm.aspx.cs主要代码:

        protected void Button1_Click(object sender, EventArgs e)
        {
            Student stu1 = new Student("2121001","彬菌");
            Student stu2 = new Student("2121002", "彬酱");
            Student stu3 = new Student("2121003", "daobin");
            List<Student> list = new List<Student>();
            list.Add(stu1);
            list.Add(stu2);
            list.Add(stu3);
            StuList stuList = new StuList();
            stuList.count = list.Count;
            stuList.data = list;
            string json = JsonConvert.SerializeObject(stuList);
            ViewState["json"] = json;
            Label1.Text = json;
        }
        protected void Button2_Click(object sender, EventArgs e)
        {
            string json = ViewState["json"].ToString();
            StuList stu = JsonConvert.DeserializeObject<StuList>(json);
            for (int i=0;i<stu.count;i++)
            {
                string info = "学号:" + stu.data[i].stuNO + "姓名:" + stu.data[i].stuName+"<hr />";
                Label2.Text += info;
            }
        }

原文链接:https://www.idaobin.com/archives/1246.html

让我恰个饭吧.ヘ( ̄ω ̄ヘ)

支付宝 ——————- 微信
图片加载中图片加载中



恰饭广告

发表评论

电子邮件地址不会被公开。 必填项已用*标注

− 2 = 6