private void button3_Click(object sender, EventArgs e)
{
List<Student> students = new List<Student>();
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<String, Object> dicList = new Dictionary<string, object>();
Dictionary<String, string> dicSampleList = new Dictionary<string, string>();
dicSampleList.Add("key_1", "111");
dicSampleList.Add("key_2", "222");
dicList.Add("SampleDatas", dicSampleList);
List<Dictionary<string, object>> tableItems = new List<Dictionary<string, object>>();
var properties = typeof(Student).GetProperties();
foreach (var stu in students)
{
Dictionary<string, object> row = new Dictionary<string, object>();
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<Dictionary<string, object>>(customJson);
var dicSample =JsonConvert.DeserializeObject<Dictionary<string, object>>(desobj["SampleDatas"].ToString());
var dicTable1 = JsonConvert.DeserializeObject<List<Dictionary<string, object>>>(desobj["Table1"].ToString());
Console.WriteLine(desobj.ToString());
MessageBox.Show(desobj.ToString());
}