JSON.NET Selecting items in array with linq JSON.NET Selecting items in array with linq json json

JSON.NET Selecting items in array with linq


If you want to get the names of all students of all teachers, you can do it for example like this:

var students = response["teacherHolder"].Children()["students"];var names = students.Children()["name"];

Or, as another option:

var names = from teacher in response["teacherHolder"]            from student in teacher["students"]            select student["name"];

If you want them as IEnumerable<string>, just add Value<string>() at the end of the select. Or add Values<string>(), if you with the first option.

But it's usually better to create types for your object model, so that you can work with them as with normal objects and not as some special JSON objects.

If you have that, you could do something like:

var names = from teacher in response.TeacherHolder            from student in teacher.Students            select student.Name;