我正在尝试找到用Java读取YAML文件的最佳方法。我不确定哪个解析器是最好的解析器,但是目前我的解决方案是读取动态(如果用户想要在文件中添加更多内容,下次程序加载时)是一系列for循环,转换为和从ArrayLists和hashmaps并解析原始字符串。
例如:
HashMap<Object, Object> perWaveHashMap = (HashMap)waveConfiguration.getList("waves").get(wave); //wave-1
for(Object mobObject : perWaveHashMap.values()){
ArrayList<Object> mobObjectArrayList = (ArrayList) mobObject;
for(Object mob : mobObjectArrayList){
HashMap<Object, Object> mobHashMap = (HashMap)mob;
String mobNameString = null;
for(Object mobName : mobHashMap.keySet()){
mobNameString = mobName.toString();
}
for(Object mobAttribute : mobHashMap.values()){
HashMap<Object, Object> attributesToGive = (HashMap)mobAttribute;
final ArrayList<Object> attributeList = new ArrayList<>();
for(Object attribute : attributesToGive.values()){
attributeList.add(attribute);
}
assert mobNameString != null;
final String finalMobNameString = mobNameString;
System.out.println("Final Mob Name String: " + finalMobNameString);
if(finalMobNameString.equalsIgnoreCase("vintr")){
Mobs.isSpecialBossWave =true;
new Vintr();
} else {
if (attributeList.size() == 1) {
Mobs.addMobs((int) attributeList.get(0), EntityType.valueOf(finalMobNameString.toUpperCase()));
} else if (attributeList.size() == 2) {
Mobs.addMobs((int) attributeList.get(0), EntityType.valueOf(finalMobNameString.toUpperCase()), (int) attributeList.get(1));
} else if (attributeList.size() == 3) {
Mobs.addMobs((int) attributeList.get(0), EntityType.valueOf(finalMobNameString.toUpperCase()), (int) attributeList.get(1), (boolean) attributeList.get(2));
} else if (attributeList.size() == 4) {
Mobs.addMobs((int) attributeList.get(0), EntityType.valueOf(finalMobNameString.toUpperCase()), (int) attributeList.get(1), (boolean) attributeList.get(2), (boolean) attributeList.get(3));
} else if (attributeList.size() == 5) {
Mobs.addMobs((int) attributeList.get(0), EntityType.valueOf(finalMobNameString.toUpperCase()), (int) attributeList.get(1), (boolean) attributeList.get(2), (boolean) attributeList.get(3), (boolean) attributeList.get(4));
} else if (attributeList.size() == 6) {
Mobs.addMobs((int) attributeList.get(0), EntityType.valueOf(finalMobNameString.toUpperCase()), (int) attributeList.get(1), (boolean) attributeList.get(2), (boolean) attributeList.get(3), (boolean) attributeList.get(4), (boolean) attributeList.get(5));
}
}
}
}
}
基本上,YML文件如下所示:
waves:
- 1:
- zombie:
amount: 1
modifier: 2
boss: false
beeline: false
- 1:
- spider:
amount: 12
modifier: 2
boss: false
beeline: false
hunter: true
-2 :
- mobname:
-options
我的代码现在的方式,允许我添加尽可能多的波浪&#39;就像我想要的那样,以及每一波浪潮中我想要的怪物。我发现我正在做代码的方式非常难看并且难以使用,我正在寻找更好的方法来解决这个问题。
欢迎任何帮助!
由于
答案 0 :(得分:1)
您可以使用snakeyaml将yaml文件读入POJO。
File configFile = new File(configFilename);
FileInputStream fis = new FileInputStream(configFile);
Yaml yaml = new Yaml(new Constructor(Config.class));
Config config = yaml.loadAs(fis, Config.class);
您的Config类可以是:
public class Config {
List<Wave> waves;
public List<Wave> getWaves() {
return waves;
}
}
// similarly define other classes for the Wave etc...