markzzz
markzzz

Reputation: 47965

How can I order a not typed array?

JSON:

"media$thumbnail":[
    {
     "url":"https://i1.ytimg.com/vi/gL23XCv6rek/default.jpg",
     "height":90,
     "width":120,
     "time":"00:08:11",
     "yt$name":"default"
    },
    {
     "url":"https://i1.ytimg.com/vi/gL23XCv6rek/mqdefault.jpg",
     "height":180,
     "width":320,
     "yt$name":"mqdefault"
    },
    {
     "url":"https://i1.ytimg.com/vi/gL23XCv6rek/hqdefault.jpg",
     "height":360,
     "width":480,
     "yt$name":"hqdefault"
    }
]

My code:

var thumbnailList = (JArray)item["media$group"]["media$thumbnail"];

and I'd like to extract the "url" with the max "width" value.

I should order this list with OrderByDescending(p => p.width), but of course I can't access to that typed value.

How can I do it? Is there a way on LINQ?

Upvotes: 0

Views: 44

Answers (1)

Joachim Isaksson
Joachim Isaksson

Reputation: 180937

What you have getting is a JArray that you can enumerate to JTokens, and JToken has a method SelectToken that can get values nested inside of it.

Using Linq, you'd end up with something similar to;

OrderByDescending(t => Convert.ToInt32(t.SelectToken("width")))

Upvotes: 1

Related Questions