我如何从所有这些节点获取值?

How do I get values from all of these nodes?

                Set<String> companies = companiesFile.getConfig().getConfigurationSection("companies").getKeys(false);

                sender.sendMessage("§2List of companies:");

                for (String s : companies)
                {
                    sender.sendMessage("§2" + companiesFile.getConfig().getString(s + ".name"));
                }

以上是我目前的代码。我正在编写一个 bukkit 插件,我正在尝试弄清楚如何从所有公司获得价值 "name"。您知道如何从所有公司获取价值 "name" 吗?这是配置:

    companies:
      simpleco:
        expenses: 3000
        revenue: 6000
        name: SimpleCo
      projectempireco:
        expenses: 5000
        revenue: 5500
        name: ProjectEmpireCo

要获取每家公司的实际名称值,您可以首先获取 companies 部分中的所有直接子键(就像您已经完成的那样),这样如果您以后向您的中添加更多顶级部分您不必遍历这些配置文件。

如果您确定每个公司都会分配一个名称值,那么您可以简单地使用直接路径(现在我们有每个公司部门的名称)和 companies.get(key + ".name"),其中 key 是公司部分的名称(例如 simpleco),公司是所有公司的 ConfigurationSection 或者您可以创建一个新的 ConfigurationSection更深一层(每个公司一个)并通过在该特定部分调用 getValues(false).get("name") 来检索键 "name" 的值。它看起来像这样:

// Get config file, here called "fileConfig"
ConfigurationSection companies = fileConfig.getConfigurationSection("companies"); // The "companies" section of the config file

for (String companyKey : companies.getKeys(false)) { // For each company key in the set
    String name = (String) companies.get(companyKey + ".name"); // Either use the path to retrieve name value

    // OR

    ConfigurationSection companySection = companies.getConfigurationSection(companyKey); // Create a new section one level deeper
    name = (String) companySection.getValues(false).get("name"); // Retrieve name by getting value of key named "name"
}