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

}

相关推荐
王维同学29 分钟前
[原创][Windows C++]Explorer Shell 扩展、图标覆盖与 COM 服务器定位
c++·windows·注册表
广州灵眸科技有限公司11 小时前
xfce桌面触摸校准:基于灵眸科技EASY-EAl-Orin-Nano
数据库·windows·科技
Ctrl+Z侠16 小时前
.NET WebApi Windows / Linux Docker 全链路压测与瓶颈定位 0 到 1 教程
linux·windows·.net
王维同学17 小时前
[原创][Windows C++]LSA 认证、安全与通知包的注册表枚举
c++·windows·安全
YCOSA202517 小时前
雨晨 Windows 11 IOT 企业版 LTSC 24H2 轻装 2合1 26100.8968
windows·物联网
汤姆yu18 小时前
CodeGeeX 4完整安装与实操使用全指南
人工智能·windows·ai·智能体·视频模型
吐了啊取名字太难20 小时前
美颜系统AI修图本地跑并支持Mac、win、安卓、iOS不卡顿
android·人工智能·windows·数码相机·mac·ai编程
筱羽_筱羽1 天前
VS2022 编译SOEM
windows·visualstudio·soem
love530love1 天前
【排障实录】GPT Desktop (Codex) 开启 WSL 智能体模式后无法启动?手把手教你修复
人工智能·windows·gpt·agent
WangWei_CM1 天前
Session Manager 的 BootExecute、SetupExecute 与 Execute 多字符串解析
windows