Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON.NET Selecting items in array with linq

I need to select some values from a json response. Im using json.net, fine with the simpler stuff, but there doesnt seem to be much documentation/tutorials on anything past that. In the json example below i need to select all the ages:

{
"teacherHolder": [{
    "id": 200000001,
    "name": "Mr Test",
    "class": "a4",
    "students": [{
        "id": "100532469",
        "name": "ben"
    },
    {
        "id": "100506025",
        "name": "bill"
    },
    {
        "id": "100000447",
        "name": "bob"
    }]

}]

}

I have tried this and other variations:

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

var names = from y in stuff.Children().Values()
                    select y["name"];

and this:

var names= response["teacherHolder"]
            .Select(s => (string)s.SelectToken("students[0].name")).ToList();

response is a JObject from a webrequest. I just get back this:

[{"Key":"Newtonsoft.Json.Linq.JEnumerable`1[Newtonsoft.Json.Linq.JToken]","Value":"Newtonsoft.Json.Linq.JEnumerable`1[Newtonsoft.Json.Linq.JToken]"}]

The results are eventually put into a dictionary.

Any idea how to do this? i know it will be simple, i just havent found the right combination.

like image 321
gdp Avatar asked Apr 02 '12 21:04

gdp


People also ask

Does LINQ work with JSON?

LINQ to JSON provides a number of methods for getting data from its objects. The index methods on JObject/JArray let you quickly get data by its property name on an object or index in a collection, while Children() lets you get ranges of data as IEnumerable<JToken> to then query using LINQ.

What is JObject and JToken?

The JToken hierarchy looks like this: JToken - abstract base class JContainer - abstract base class of JTokens that can contain other JTokens JArray - represents a JSON array (contains an ordered list of JTokens) JObject - represents a JSON object (contains a collection of JProperties) JProperty - represents a JSON ...

What is Newtonsoft JSON LINQ?

LINQ to JSON is an API for working with JSON objects. It has been designed with LINQ in mind to enable quick querying and creation of JSON objects. LINQ to JSON sits under the Newtonsoft. Json.

What is JToken in C#?

JToken is the abstract base class of JObject , JArray , JProperty , and JValue , which represent pieces of JSON data after they have been parsed. JsonToken is an enum that is used by JsonReader and JsonWriter to indicate which type of token is being read or written.


1 Answers

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;
like image 136
svick Avatar answered Nov 26 '22 05:11

svick