如何从 Json.NET 获取密钥列表?

How can I get a list of keys from Json.NET?(如何从 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 获取密钥列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)