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

}

相关推荐
nashane9 小时前
HarmonyOS长列表scrollToIndex性能优化全解析
windows·harmonyos 5
IT技术分享社区9 小时前
科技资讯:微软Win11再添新功能,屏幕色调可自定义,还能缓解眼疲劳
windows·科技·microsoft·微软·业界资讯
紫小米9 小时前
Pydantic讲解
windows
x***r15118 小时前
node-v0.12.2-x86安装步骤详解(附Node.js历史版本与32位配置教程)
windows
小小小米粒18 小时前
Collection单列集合、Map(Key - Value)双列集合,多继承实现。
java·开发语言·windows
蜡台19 小时前
Python包管理工具pip完全指南-----2
linux·windows·python
love530love1 天前
Windows Podman Machine 虚拟硬盘迁移完整指南:从 C 盘到非系统盘
c语言·人工智能·windows·podman
love530love1 天前
Podman Machine 虚拟硬盘迁移实战二:用 Junction 把 vhdx 从 C 盘搬到其他盘
c语言·开发语言·人工智能·windows·wsl·podman·podman machine
薛定猫AI1 天前
【技术干货】Claude Code 终端编程实战:从零搭建 Windows 高效 AI 开发环境
人工智能·windows
V我五十买鸡腿1 天前
网安基础 Windows 和 Linux 那些常用命令
linux·运维·windows