如何从 Json.NET 获取密钥列表? [英] How can I get a list of keys from Json.NET?

查看:20
本文介绍了如何从 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 获取密钥列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆