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

}

相关推荐
tryCbest1 天前
Windows和Linux设置Https(SSL)访问
linux·windows·https
奔跑吧邓邓子1 天前
【C++实战(68)】从0到1:C++跨平台开发之Windows API深度实战
c++·windows·实战·跨平台·windows api
名誉寒冰1 天前
# 深入理解Linux内核与用户态通信:Netlink机制实战
linux·服务器·windows
byte轻骑兵1 天前
Windows 安全分割利器:strtok_s () 详解
c语言·开发语言·windows·安全
染指11102 天前
11.UE-游戏逆向-内存中的FUObjectArray(深入理解内存数据)
windows·虚幻·ue·unreal engine 4
TeleostNaCl2 天前
使用 jintellitype 库在 Java 程序中实现监听 Windows 全局快捷键(热键)
java·开发语言·windows·经验分享·kotlin·电脑
祈祷苍天赐我java之术2 天前
Redis 有序集合解析
java·前端·windows·redis·缓存·bootstrap·html
你好龙卷风!!!2 天前
mac | Windows 本地部署 Seata1.7.0,Nacos 作为配置中心、注册中心,MySQL 存储信息
windows·mysql·macos
恸流失2 天前
java基础-12 : 单列集合(Collection)
java·开发语言·windows
做运维的阿瑞3 天前
Windows 环境下安装 Node.js 和 Vue.js 框架完全指南
前端·javascript·vue.js·windows·node.js