json序列化和反序列化List

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Newtonsoft.Json;

namespace JsonDome
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                List listObj = new List() { 
                    new Students {name="杨飞宰",age=25, sex="男"},
                    new Students {name="古天乐",age=44, sex="男"},
                    new Students {name="李汉平",age=25, sex="男"},
                    new Students {name="刘亦菲",age=27, sex="女"}
                };

                JsonData> obj = new JsonData>();
                obj.adminKid = 1;
                obj.buildingKid = 10;
                obj.appkey = "xxxxxxxxxxxxxxxxx";
                obj.ver = "1.0";
                obj.token = "xxxxxxxxxxxxxxxxx";
                obj.list = listObj;

                //先序列化Json格式数据
                string jsonText = JsonConvert.SerializeObject(obj);

                //反序列化对象
                JsonData> json = JsonConvert.DeserializeObject>>(jsonText);
            }
        }
    }

    /// 
    /// JSON数据类
    /// 
    /// 
    public class JsonData
    {
        #region 系统参数
        public int adminKid { get; set; }
        public int buildingKid { get; set; }
        public string appkey { get; set; }
        public string ver { get; set; }
        public string token { get; set; }
        public T list { get; set; }
        #endregion
    }

    /// 
    /// 学生实体
    /// 
    [Serializable]
    public class Students
    {
        public string name { get; set; }
        public int age { get; set; }
        public string sex { get; set; }
    }
}


 

你可能感兴趣的:(json序列化和反序列化List)