本文介绍了使用JSON.NET库在JArray中查找节点(JObject)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JSON.NET库.我已经创建了几个JObjects并将它们添加到JArray中.

I am using JSON.NET library. I have created few JObjects and added them to a 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);

现在,我想查找文本(JProperty)为Two的JObject.如何通过使用JProperty在JArray中找到JObject.

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 jo = array.Children<JObject>()
    .FirstOrDefault(o => o["text"] != null && o["text"].ToString() == "Two");

这将在JArray中找到具有名为text且值为Two的属性的第一个JObject.如果不存在这样的JObject,则jo将为空.

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.

这篇关于使用JSON.NET库在JArray中查找节点(JObject)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 05:30