简体   繁体   中英

Java - Reading all strings from a list in .YML

I was wondering whether it would be possible to read, from a YAML file, and get all the items into a list: It's pretty hard to explain, but i'll provide an example. ArrayList lists = ???: will provide all the strings inside of a YAML file which has inside

players:
  Y: 0
  X: 8
  Z: 0

etc.

Is there specific code to actually get just the strings, so example lists would equal to Y, X and Z, as strings and be modifiable.

Really appreciate help on this one :) Thanks!

You can use any yaml parser. For example JYaml . The yaml in the question is basicaly a Map with key "players" and value another map. So using JYaml you could do like Object object = Yaml.load(new File("data.yml")); . By default, JYaml converts a sequence to java.util.ArrayList and a mapping to java.util.HashMap. So we could do like

Map<String, Map<String, String>> object = (HashMap<String, Map<String, String>>) Yaml.load(new File("data.yml"));

and get the key or values like

object.get("players").keySet(); // Y, X, Z
object.get("players").values(); // 8, 0, 0

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