首页 > 代码库 > Newtonsoft.Json.JsonConvert 序列化与反序列化

Newtonsoft.Json.JsonConvert 序列化与反序列化

技术分享
private void Form6_Load(object sender, EventArgs e)
        {
            stud1 objstud1 = new stud1() { id = 100, sex = "", birthday = new DateTime(1972, 5, 29),fname="zhang" };
            string str= Newtonsoft.Json.JsonConvert.SerializeObject(objstud1);
            stud0 objstu0 = Newtonsoft.Json.JsonConvert.DeserializeObject<stud0>(str) ; //正确返回
            stud2 objstu2 = Newtonsoft.Json.JsonConvert.DeserializeObject<stud2>(str);  //正确返回
            stud2 objstu3 = Newtonsoft.Json.JsonConvert.DeserializeObject(str) as stud2; //返回null
        }
        public class stud0
        {
            public int id;
            public string sex;
        }
        public class stud1
        {
            public int id;
            public string sex;
            public DateTime birthday;
            public string fname;

        }
        public class stud2
        { 
            public int id;
            public string sex;
            public DateTime birthday;
            public string mname;
        }
View Code

 

Newtonsoft.Json.JsonConvert 序列化与反序列化