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

}

相关推荐
程序猿小D10 小时前
第二百三十五节 JPA教程 - JPA Lob列示例
java·数据库·windows·oracle·jdk·jpa
iummature12 小时前
ZLMediaKit Windows编译以及使用
windows
周伯通*15 小时前
Windows上,使用远程桌面连接Ubuntu
linux·windows·ubuntu
GDAL18 小时前
GNU力量注入Windows:打造高效跨平台开发新纪元
服务器·windows·gnu
小徐敲java21 小时前
Windows本地制作java证书(与jeecgboot配置本地证书ssl问题)
java·windows·ssl
春蕾夏荷_72829772521 小时前
electron nsis打包windows应用程序
javascript·windows·electron·nsis
偷偷小野猪21 小时前
Windows 常用的键盘快捷键总结
windows
Splashtop高性能远程控制软件1 天前
centos远程桌面连接windows
linux·windows·centos·远程控制·远程桌面
楚钧艾克1 天前
Windows系统通过部署wsl + Goland进行跨平台开发
linux·windows·后端·ubuntu·golang
wow2ok1 天前
天融信把桌面explorer.exe删了,导致开机之后无windows桌面,只能看到鼠标解决方法
windows·计算机外设