我正在使用 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 获取密钥列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!