码迷,mamicode.com
首页 > Windows程序 > 详细

C#中的各种json取值

时间:2017-10-18 18:30:20      阅读:307      评论:0      收藏:0      [点我收藏+]

标签:where   soft   获得   col   https   list   集合   class   def   

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") == "女?");

 

C#中的各种json取值

标签:where   soft   获得   col   https   list   集合   class   def   

原文地址:http://www.cnblogs.com/chen-yuan/p/7687924.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!