I have a JSON
{
"departments": [
{
"2": {"city": "Petersburg", "employees": "1200"}
},
{
"1": {"city": "Ekaterinburg", "employees": "4000"}
}
]
}
How can I get the value of the city, if I know an ID using LINQ or something else?
I tried
var id = 2;
json["departments"].Single(x=>x.Name==id.ToString())["city"];
But it doesn't work, I get a compilation error:
'JToken' does not contain a definition for 'Name' and no accessible extension method 'Name' accepting a first argument of type 'JToken' could be found (are you missing a using directive or an assembly reference?)
Demo fiddle here.