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

}

相关推荐
学海浪太大5 小时前
笔记本电脑插入显示器没反应
windows
黄一一9112436 小时前
告别手动敲字,本地截图 OCR,识别完直接复制!开源截图识别工具
windows·ocr·开源软件·贴图
洪流之源8 小时前
Windows 远程 Ubuntu 24.04 桌面
linux·windows·ubuntu
是小李呀9 小时前
Windows 虚拟内存配置完全指南:从原理到实操,彻底解决内存不足与 OOM 问题
windows
毕竟是shy哥11 小时前
windows电脑WSL下本地构建docker镜像
windows·docker·容器
kakakahahahaha20 小时前
【Windows】C盘低空间反复复发的排查与扩容边界
c语言·windows·电脑·笔记本电脑·内容运营·软件需求
2301_800074211 天前
map,list简单方法
windows·python·list
技术不好的崎鸣同学1 天前
Windows 红队实战:前言
windows
吴声子夜歌1 天前
Guava——基本工具(二)
windows·python·guava
猎嘤一号1 天前
【2026 最新】Windows 11 右键菜单还原为 Windows 10 经典样式:一条命令、原理、回退与新版说明
windows·python