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

}

相关推荐
你是人间五月天9 小时前
sentinel实现控制台与nacos数据双向绑定
windows·sentinel
小猫挖掘机(绝版)12 小时前
通过tailscale实现一台电脑上vscode通过ssh连接另一台电脑上的VMware Linux 虚拟机
linux·windows·vscode·ubuntu·ssh
你我约定有三18 小时前
java--泛型
java·开发语言·windows
self_myth18 小时前
[特殊字符] 深入理解操作系统核心特性:从并发到分布式,从单核到多核的全面解析
windows·macos·wpf·harmonyos
十五年专注C++开发20 小时前
cargs: 一个轻量级跨平台命令行参数解析库
linux·c++·windows·跨平台·命令行参数解析
小韩博20 小时前
Windows权限提升(二)
windows·网络安全·github
CookieCrusher1 天前
数据泄露危机逼近:五款电脑加密软件为企业筑起安全防线
运维·数据库·windows·安全·文件加密·数据防泄漏·dlp
lvcoc1 天前
unity 接入火山引擎API,包括即梦AI
windows·unity·ai·火山引擎
vortex51 天前
AD渗透中服务账号相关攻击手法总结(Kerberoasting、委派)
windows·网络安全·渗透测试·ad