C#不建类直接Json解析与取值
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lily233/article/details/78285908
1、添加引用Newtonsoft.Json.dll(附件:https://files.cnblogs.com/files/chen-yuan/Newtonsoft.zip);
2、引用:
using Newtonsoft.Json.Linq;
- 1
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();