简体   繁体   English

Java-从.YML中的列表读取所有字符串

[英]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. 我想知道是否有可能从YAML文件中读取并将所有项目都放入列表中:很难解释,但我将提供一个示例。 ArrayList lists = ???: will provide all the strings inside of a YAML file which has inside ArrayList listings = ???:将提供YAML文件内部的所有字符串,该文件内部

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. 是否有特定的代码实际上只获取字符串,所以示例列表将Y,X和Z等同为字符串,并且可以修改。

Really appreciate help on this one :) Thanks! 真的很感谢这方面的帮助:)谢谢!

You can use any yaml parser. 您可以使用任何yaml解析器。 For example JYaml . 例如JYaml The yaml in the question is basicaly a Map with key "players" and value another map. 问题中的Yaml基本上是一张带有关键“玩家”的地图,并重视另一张地图。 So using JYaml you could do like Object object = Yaml.load(new File("data.yml")); 因此,使用JYaml,您可以像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. 默认情况下,JYaml将序列转换为java.util.ArrayList,并将映射转换为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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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