2017年5月28日 星期日

jason序列化

一、加入參考


WebForm1.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Newtonsoft.Json;
using jsaon_test1.Models;

namespace jsaon_test1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
           
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            //List_persons lisp = new List_persons();
            //lisp.listpersons = data_jason();
            //string output = Newtonsoft.Json.JsonConvert.SerializeObject(lisp.listpersons);
            //Literal1.Text = output;
            List<person> prs = new List<person>();
            prs = data_jason();
            string output = Newtonsoft.Json.JsonConvert.SerializeObject(prs);
            Literal1.Text = output;
        }
        public List<person> data_jason()
        {
            person pr1 = new person();
            pr1.Age = 18;
            pr1.Name = "tony";
            pr1.birthday = Convert.ToDateTime("2007-8-1");
            person pr2 = new person();
            pr2.Age = 19;
            pr2.Name = "mary";
            pr2.birthday = Convert.ToDateTime("2008-8-1");
            List<person> data = new List<person>();
            data.Add(pr1);
            data.Add(pr2);
            return data;
        }

        protected void Button2_Click(object sender, EventArgs e)
        {

        }
    }
}
person.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace jsaon_test1.Models
{
    public class person
    {
        public int Age { get; set; }
        public string Name { get; set; }
        public DateTime birthday { get; set; }
    }
}


沒有留言:

張貼留言