Java从config.yml中获取Maps的List of maps

标签 java yaml minecraft bukkit

我正在尝试创建一个插件,用于存储一些 Minecraft 元素的数据以及一些属性。

这是我的 YAML 文件的内容:

rates:
- 391:
    mul: 10000
    store: 5000
- 392:
    mul: 9000
    store: 5000

所以它基本上是 map 的 map 列表(至少我是这么认为的)。 这是我的 JAVA 代码,我在其中尝试访问“391”的键“mul”:

List<Map<?,?>> rates;
rates= getConfig().getMapList("rates");
for(Map<?,?> mp : rates){
    Map <?,?> test = (Map<?,?>) mp.get("" + item);
    player.sendMessage(test.toString());// HERE I get null pointer exception, and the following lines if this line wasn't there in the first place
    player.sendMessage("Mul is: " + test.get("mul"));
    player.sendMessage("Store is: " + test.get("store"));
}

根据建议的答案,这是我的测试代码,我仍然得到 NullPointerException:

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Map;

import net.sourceforge.yamlbeans.YamlException;
import net.sourceforge.yamlbeans.YamlReader;

public class Test {

public static void main(String[] args) throws FileNotFoundException, YamlException{
    YamlReader reader = new YamlReader(new FileReader("config.yml"));
    Map map = (Map) reader.read();
    Map itemMap = (Map) map.get("391");
    System.out.println(itemMap.get("mul"));//This is where I get the exception now
    System.out.println(itemMap.get("store"));
}
}

最佳答案

手动解析 yaml 既乏味又容易出错。使用像 yamlbeans 这样的库可能会更容易。

http://yamlbeans.sourceforge.net/

package com.jbirdvegas.q41267676;

import com.esotericsoftware.yamlbeans.YamlReader;

import java.io.StringReader;
import java.util.List;
import java.util.Map;

public class YamlExample {
    public static void main(String[] args) throws Exception {

        String yamlInput =
                "rates:\n" +
                        "- 391:\n" +
                        "    mul: 10000\n" +
                        "    store: 5000\n" +
                        "- 392:\n" +
                        "    mul: 9000\n" +
                        "    store: 5000";

        YamlReader reader = new YamlReader(new StringReader(yamlInput));
        Map map = (Map) reader.read();
        // rates is a list
        List<Map> rates = (List<Map>) map.get("rates");
        // each item in the list is a map
        for (Map itemMap : rates) {
            // each map contains a single map the key is [ 391|392 ]
            itemMap.forEach((key, value) -> {
                System.out.println("Key: " + key);
                // the value in in this map is itself a map
                Map embededMap = (Map) value;
                // this map contains the values you want
                System.out.println(embededMap.get("mul"));
                System.out.println(embededMap.get("store"));
            });
        }
    }
}

这打印:

Key: 391
10000
5000
Key: 392
9000
5000

这是一个简单的用例,但 yamlbeans 还提供了 GSON,如反射类模型填充,如果它更适合您的需求的话。

关于Java从config.yml中获取Maps的List of maps,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41267676/

相关文章:

Java:while 循环中的 For 循环不会运行,除非包含 println 语句

java - volatile 与原子性

java - 使用 Restful 和 CDI 协同工作的最佳方式是什么?

python - 在pyyaml中表示具有相同基类的不同类的实例

ssh - Windows ssh - 如何在断开连接后保持进程运行

java - IntelliJ IDEA 错误 : Could not find or load main class GradleStart

java - Netty 中的 ByteBuffers 导致内存泄漏

Python、yaml嵌套对象

yaml - 使用 yaml 在 liquibase 的前提条件下倍数 dbms

java - 出于 mod 测试目的,Minecraft 不会从 eclipse 启动