Finding a node (JObject) in a JArray using the JSON.NET library

I am using the JSON.NET library. I created several JObjects and added them to JArray.

JArray array = new JArray(); JObject obj = new JObject(); obj.Add(new JProperty("text", "One")); obj.Add(new JProperty("leaf", false)); array.Add(obj); obj = new JObject(); obj.Add(new JProperty("text", "Two")); obj.Add(new JProperty("leaf", false)); array.Add(obj); obj = new JObject(); obj.Add(new JProperty("text", "Three")); obj.Add(new JProperty("leaf", true)); array.Add(obj); 

Now I want to find a JObject whose text (JProperty) is Two . How to find a JObject in a JArray using JProperty.

+6
source share
1 answer

You can find it as follows:

 JObject jo = array.Children<JObject>() .FirstOrDefault(o => o["text"] != null && o["text"].ToString() == "Two"); 

This will find the first JObject in the JArray having a property named text with a value of Two . If such a JObject does not exist, then jo will be null.

+15
source

Source: https://habr.com/ru/post/957185/


All Articles