2016-01-31 29 views
-1
   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插件,我正試圖弄清楚如何從所有公司獲得「名稱」的價值。你知道如何從所有公司獲得「名字」的價值嗎?下面是配置:如何從所有這些節點獲取值?

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

'getValues'should工作https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/configuration/ConfigurationSection.html#getValues(boolean) – Bajal

+0

@Bajal如何你使用getValues()方法嗎? –

回答

1

爲了讓各公司的實際名稱值,你能先取得在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" 
} 
+0

非常感謝! :) –

+0

很高興我能幫忙!如果答案解決了您的問題,您是否可以使用複選標記將其標記爲「已接受」答案?這樣每個人都知道你的問題已經解決。謝謝! –

相關問題