Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get element by key in JSON array

Tags:

json

c#

I have the following JArray object (newtonsoft.json)

[
  {
    "Administrator": 3
  },
  {
    "User": 1
  },
  {
    "Guest": 5
  }
]

How do I retrieve the value (3) for key "Administrator" ? It's asking me for an array index but I would like to retrieve by key.. as this list can expand and contract..

like image 792
user5120455 Avatar asked Dec 18 '22 12:12

user5120455


1 Answers

Using Json.Net you can simply do

int value = (int)JArray.Parse(json).Children()["Administrator"].First();
like image 99
L.B Avatar answered Dec 24 '22 01:12

L.B