4

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..

3
  • 1
    Well, if you had access to a a Turing complete programming language, you could use LINQ or a loop to search the array for an object with a property named Adminstrator. Commented Oct 27, 2016 at 19:52
  • 5
    this json is invalid Commented Oct 27, 2016 at 19:53
  • This question would probably be better titled, "How to get a property of an object in an array of objects?" You're not actually going to be working with a JSON string, but rather the array of objects parsed from the JSON. Commented Oct 27, 2016 at 20:28

4 Answers 4

7

Using Json.Net you can simply do

int value = (int)JArray.Parse(json).Children()["Administrator"].First();
Sign up to request clarification or add additional context in comments.

3 Comments

Grr definitely better answer !
@mybirthname right? These are all good ... I love the one-liners though.
"simply do.." 😂
2
  string json = @"[
  {
    ""Administrator"": 3
  },
  {
    ""User"": 1
  },
  {
    ""Guest"": 5
  }
]";

JArray jsonObject = JsonConvert.DeserializeObject<JArray>(json);

var adminVal = jsonObject[0]["Administrator"].Value<int>();

Like in comments said the JSON is invalid. Here is the fixed version and how to take the Administrator value.

EDIT

Here how to do it without specify the index of the JArray.

var adminObject = jsonObject.Children<JObject>().FirstOrDefault(x=>x.Children<JProperty>().Any(y=>y.Name == "Administrator"));
var adminObjectValue = adminObject.GetValue("Administrator");

1 Comment

I'm trying to do this without the array index.. because the list can increase/decrease in size and adminstrator might not always be at index '0'
2

You could first read your Json as a list of dictionaries using Json.NET and then merge them via linq:

var json = @"[
  {
    ""Administrator"": 3
  },
  {
    ""User"": 1
  },
  {
    ""Guest"": 5
  }
]";

var list = JsonConvert.DeserializeObject<List<Dictionary<string, int>>>(json);
var dict = list.SelectMany(d => d).ToDictionary(p => p.Key, p => p.Value);
var adminId = dict["Administrator"];

2 Comments

Which namespace is JsonCovert?
@user5120455 forgot to mention. The library is called Json.NET, I update my answer
1

Your Json should be.

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

You can desrilize this json using NewtonSoft.Json dll to the following class. and get the value of Administrator with class object.

public class ClassName
{
    public int Administrator { get; set; }
    public int? User { get; set; }
    public int? Guest { get; set; }
}

Code to desrialize json to class object.

ClassName obj = JsonConvert.DeserializeObject<ClassName>(json);
//obj.Administrator get or set it according to your requirement.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.