C# 使用 JSON.NET 库在 JArray 中查找节点 (JObject)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19726121/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Finding a node (JObject) within JArray using JSON.NET library
提问by SharpCoder
I am using JSON.NET library. I have created few JObjects and added them to a JArray.
我正在使用 JSON.NET 库。我创建了几个 JObject 并将它们添加到 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 who's text (JProperty) is Two
. How can I find a JObject within a JArray by using a JProperty.
现在我想找到一个 JObject,它的文本 (JProperty) 是Two
. 如何使用 JProperty 在 JArray 中找到 JObject。
采纳答案by Brian Rogers
You can find it like this:
你可以这样找到它:
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 no such JObject
exists, then jo
will be null.
这将JObject
在JArray
具有以text
值命名的属性中找到第一个Two
。如果不JObject
存在,jo
则为空。