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

}

相关推荐
2301_789380492 小时前
【踩坑记录】Windows资源管理器无限重启,改注册表、看日志
windows·经验分享·笔记
sukalot2 小时前
Windows 驱动实例分析系列:libwdi 驱动分析 - libwdi 篇(五)
windows·驱动开发
(Charon)2 小时前
【C/C++面试】手写内存池:从空闲链表到内存申请与释放
java·开发语言·windows
CS创新实验室3 小时前
Python 的鸭子类型
开发语言·windows·python
kakakahahahaha3 小时前
电脑显示器无信号排查:HDMI/DP接口、独立显卡、内存与安全模式
windows·安全·电脑·笔记本电脑·内容运营·软件需求
ITHAOGE1516 小时前
下载 | Win10 2021纯净精简版,预装应用极少!(9月更新、Win10 IoT LTSC 2021版、适合老电脑)
windows·科技·物联网·微软·电脑
Vex2une20 小时前
Windows 任务管理器发展史:从 80KB 救急工具到系统级监控平台
windows·进程管理·性能监控·任务管理器·winui·cpu使用率·taskmgr
Vex2une21 小时前
Windows 12 新功能展望:AI 深度融合、全新界面与下一代生产力体验
人工智能·windows·copilot·winui·新功能·ai pc
LRL_1 天前
【实战指南】Node.js 跨平台依赖下载:如何在 Windows/Linux 环境下互跨下载目标系统的 npm/pnpm 包
linux·windows·node.js
言乐61 天前
Python文字审核模型
开发语言·windows·python·django·pygame