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

}

相关推荐
LZQqqqqo2 小时前
C# 中 ArrayList动态数组、List<T>列表与 Dictionary<T Key, T Value>字典的深度对比
windows·c#·list
季春二九2 小时前
Windows 11 首次开机引导(OOBE 阶段)跳过登录微软账户,创建本地账户
windows·microsoft
芥子沫3 小时前
Jenkins常见问题及解决方法
windows·https·jenkins
cpsvps_net19 小时前
美国服务器环境下Windows容器工作负载智能弹性伸缩
windows
甄超锋20 小时前
Java ArrayList的介绍及用法
java·windows·spring boot·python·spring·spring cloud·tomcat
cpsvps1 天前
美国服务器环境下Windows容器工作负载基于指标的自动扩缩
windows
网硕互联的小客服1 天前
Apache 如何支持SHTML(SSI)的配置方法
运维·服务器·网络·windows·php
etcix1 天前
implement copy file content to clipboard on Windows
windows·stm32·单片机
许泽宇的技术分享1 天前
Windows MCP.Net:基于.NET的Windows桌面自动化MCP服务器深度解析
windows·自动化·.net
非凡ghost1 天前
AMS PhotoMaster:全方位提升你的照片编辑体验
windows·学习·信息可视化·软件需求