在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
1、添加引用Newtonsoft.Json.dll(附件:https://files.cnblogs.com/files/chen-yuan/Newtonsoft.zip);
2、引用:
using Newtonsoft.Json.Linq;
3、具体代码: string students = "{\"grade\":\"6\",\"class\":\"1\",\"students\":[{ \"id\":\"1\",\"name\":\"Lily\",\"sex\":\"女?\"},{\"id\":\"2\",\"name\":\"Jack\",\"sex\":\"男D\"},{\"id\":\"3\",\"name\":\"Lucy\",\"sex\":\"女?\"}]}"; JObject studentsJson = JObject.Parse(students); //年级 string grade = studentsJson["grade"].ToString(); //获得第二个学生的姓名 string name1 = studentsJson["students"][1]["name"].ToString(); //Or name1 = studentsJson["students"].AsEnumerable().ElementAt(1)["name"].ToString(); //遍历学生信息 var studentsList = studentsJson["students"].AsEnumerable(); foreach (var item in studentsList) { string a = item["name"].ToString(); } //获得学生Jack的信息和性别 var Jack = studentsJson["students"].AsEnumerable().Where(t => t.Value<string>("name") == "Jack"); string Jack_sex = Jack.FirstOrDefault()["sex"].ToString(); //获得女生的信息集合 var Girls = studentsJson["students"].AsEnumerable().Where(t => t.Value<string>("sex") == "女?");
在不确定key的情况下要遍历所有数据,在网上找到的,自己Mark一下 1、添加引用Newtonsoft.Json.dll(附件:https://files.cnblogs.com/files/chen-yuan/Newtonsoft.zip);
2、引用:
using Newtonsoft.Json.Linq;
3、具体代码: string students = "{\"grade\":\"6\",\"class\":\"1\",\"students\":[{ \"id\":\"1\",\"name\":\"Lily\",\"sex\":\"女?\"},{\"id\":\"2\",\"name\":\"Jack\",\"sex\":\"男D\"},{\"id\":\"3\",\"name\":\"Lucy\",\"sex\":\"女?\"}]}"; JObject studentsJson = JObject.Parse(students); //年级 string grade = studentsJson["grade"].ToString(); //获得第二个学生的姓名 string name1 = studentsJson["students"][1]["name"].ToString(); //Or name1 = studentsJson["students"].AsEnumerable().ElementAt(1)["name"].ToString(); //遍历学生信息 var studentsList = studentsJson["students"].AsEnumerable(); foreach (var item in studentsList) { string a = item["name"].ToString(); } //获得学生Jack的信息和性别 var Jack = studentsJson["students"].AsEnumerable().Where(t => t.Value<string>("name") == "Jack"); string Jack_sex = Jack.FirstOrDefault()["sex"].ToString(); //获得女生的信息集合 var Girls = studentsJson["students"].AsEnumerable().Where(t => t.Value<string>("sex") == "女?");
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论