How can I get a list of keys from Json.NET?(如何从 Json.NET 获取密钥列表?)
问题描述
我正在使用 C# 和 Json.NET.如果我有一个 JObject,我想要一个对象内键的列表,类似于 object.Keys()
如何返回对象内的键.这似乎很明显,但我很难找到一种方法来做到这一点.
I'm using C# and Json.NET. If I have a JObject, I want a list of the keys within the object, similar to how object.Keys()
returns the keys within the object. This seems like it'd be obvious, but I'm having a rough time finding a way to do this.
我正在遍历对象,我想在遍历时吐出对象中的所有键.我意识到这个例子会导致多次看到同一个键,这符合我的需要.
I'm traversing through the object, and I want to spit out all the keys in the object as I go through. I realize that this example will result in seeing the same key multiple times, and that's OK for my needs.
public void SomeMethod(JObject parent) {
foreach (JObject child in parent.Children()) {
if (child.HasValues) {
//
// Code to get the keys here
//
SomeMethod(child);
}
}
}
推荐答案
IList<string> keys = parent.Properties().Select(p => p.Name).ToList();
文档:JObject.Properties
这篇关于如何从 Json.NET 获取密钥列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何从 Json.NET 获取密钥列表?


基础教程推荐
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01