Finding a node (JObject) within JArray using JSON.NET library(使用 JSON.NET 库在 JArray 中查找节点 (JObject))
问题描述
我正在使用 JSON.NET 库.我创建了几个 JObject 并将它们添加到 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 中找到第一个 JObject,它的属性名为 text,其值为 Two.如果不存在这样的 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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 JSON.NET 库在 JArray 中查找节点 (JObject)
基础教程推荐
- 如何使用 .Net 检查 Active Directory 服务器是否已启动并正在运行? 2022-01-01
- 如果有人提交恶意软件Nuget包怎么办? 2022-01-01
- Azure Functions:CosmosDBTrigger 未在 Visual Studio 中触发 2022-01-01
- .NET SerialPort DataReceived 事件未触发 2022-01-01
- C# 从 List<List<int>> 中删除重 2022-01-01
- WPF 模态进度窗口 2022-01-01
- 我应该在后面的代码中直接使用 Linq To SQL 还是使 2022-01-01
- 当值可以是对象或空数组时反序列化 JSON 2022-01-01
- Moq It.Is<>不匹配 2022-01-01
- 禁止输入少量字符,例如'<'、'&a 2022-01-01
