Newtonsoft.Json use


        private void button3_Click(object sender, EventArgs e)
        {
            List students = new List();

            students.Add(new Student { Id = 1, Name = "张三", Sex = "男", Description = "班长" });

            students.Add(new Student { Id = 2, Name = "李四", Sex = "女", Description = "小组长" });

            students.Add(new Student { Id = 3, Name = "王五", Sex = "男", Description = "宣传委员" });


            string studentsJson = JsonConvert.SerializeObject(students);
            MessageBox.Show(studentsJson);
            Console.WriteLine(studentsJson);


            //
            Dictionary dicList = new Dictionary();
            Dictionary dicSampleList = new Dictionary();
            dicSampleList.Add("key_1", "111");
            dicSampleList.Add("key_2", "222");
            dicList.Add("SampleDatas", dicSampleList);
            List> tableItems = new List>();
            var properties = typeof(Student).GetProperties();

            foreach (var stu in students)
            {
                Dictionary row = new Dictionary();
                foreach (System.Reflection.PropertyInfo info in properties)
                {
                    var value = stu.GetType().GetProperty(info.Name).GetValue(stu, null);
                    //Console.WriteLine(info.Name,value);
                    row.Add(info.Name,value);
                    
                }
                tableItems.Add(row);
            }
            
            dicList.Add("Table1", tableItems);
            string customJson = JsonConvert.SerializeObject(dicList);
            MessageBox.Show(customJson);
            Console.WriteLine(customJson);

            var desobj = JsonConvert.DeserializeObject>(customJson);
            var dicSample =JsonConvert.DeserializeObject>(desobj["SampleDatas"].ToString());
            var dicTable1 = JsonConvert.DeserializeObject>>(desobj["Table1"].ToString());

            Console.WriteLine(desobj.ToString());
            MessageBox.Show(desobj.ToString());
        }
      

你可能感兴趣的:(windows)