我怎样才能从Json.NET键列表? [英] How can I get a list of keys from Json.NET?

查看:87
本文介绍了我怎样才能从Json.NET键列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用C#和Json.NET。如果我有一个JObject,我想要的对象中的按键,类似于如何 object.Keys()返回对象中的密钥列表。这似乎是它会是显而易见的,但我有一个粗略的时间寻找一种方法来做到这一点。

编辑:
我穿越过的对象,我要吐了出来对象中的所有的钥匙,我去。我意识到,这个例子将导致相同的密钥多次看到,这是确定了我的需求。

 公共无效的someMethod(JObject父){
    的foreach(JObject儿童parent.Children()){
        如果(child.HasValues​​){
        //
        // code到拿到钥匙在这里
        //
        的someMethod(小孩);
        }
    }
}


解决方案

 的IList<串GT;键= parent.Properties()选择(P => p.Name)。.ToList();

文件:<一href=\"http://www.newtonsoft.com/json/help/html/JObjectProperties.htm\">JObject.Properties

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.

Edit: 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();

Documentation: JObject.Properties

这篇关于我怎样才能从Json.NET键列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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