简体   繁体   中英

How to get all of the entries in a yaml file?

I am a bit clueless with how to get all of the entries in my stats yaml file here is what it looks like

PLAYERUUID:
  kills: 7
  deaths: 5
  points: 0
  wins: 0
  losses: 1
PLAYERUUID2:
  kills: 14
  deaths: 10
  points: 1
  wins: 1
  losses: 4

etc. How would I get all both playerUUID and playerUUID2 so I could use it in a for loop? Would I need to add an external manager?

The best way to do this depends on what your ultimate goal is, but if you just want to loop through all the keys you can use the getKeys() method:

yml.getKeys(true) //With yml being an instance of your configuration file

This will create a Set<String> of all the keys in the config, which you can iterate through. The true parameter specifies that you want to loop through the subkeys as well. If you only want to iterate through the primary keys, set it to false .

To then retrieve the values of the keys, you can do something like this:

for (String key : yml.getKeys(true)) {
    int value = yml.getInt(key); //Assumes all the values in your config are integers
    System.out.println(key + ": " + value);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM