Newtonsoft.Json use

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());

}

相关推荐
donglxd2 小时前
老电脑优化全知道(包括软件和硬件优化)
运维·windows·系统安全
ZXF-BW7 小时前
联想笔记本电脑在Windows下通过联想驱动实现风扇控制
windows·电脑·联想·风扇
csdn_aspnet9 小时前
如何在 Windows 10 中使用 WSL 和 Debian 安装 Postgresql 和 Postgis
windows·postgresql·debian
九鼎科技-Leo9 小时前
写windows服务日志-.net4.5.2-定时修改数据库中某些参数
数据库·windows·.net
扛枪的书生1 天前
AD 侦查-MSRPC
windows·渗透·kali·提权·域渗透
程序员Bears1 天前
IntelliJ IDEA 2025系列通用软件安装教程(Windows版)
ide·windows·pycharm·webstorm
CHANG_THE_WORLD1 天前
Windows DirectWrite接口 遍历字体信息
windows
只可远观1 天前
Flutter Dart 集合类型List Set Map详解军 以及循环语句 forEaclh map where any every
windows·flutter·list
yangshuo12811 天前
风车邮箱系统详细使用指南:Windows与Ubuntu双平台解析
linux·windows·ubuntu
技术liul1 天前
如何在iStoreOS DHCP中排除特定IP地址
网络·windows·tcp/ip