对于具有多个对象的 YAML 数组,在 Java 中使用什么路径?

发布于 2025-01-10 04:39:43 字数 663 浏览 0 评论 0原文

我目前正在尝试弄清楚如何将字符串从 config.yml 获取到我的 Java 代码中。更具体地说,使用哪个路径来获取“Home”“X:-1 Y:79 Z:3”“Nether”“X:23 Y:65 Z:-19”提取到我的Java代码中。我已经尝试过 getConfig().getString("Streuzel[0].name"),但这不起作用。

YAML:(具有多个对象的数组)

Streuzel:
    -
        name: Home
        coords: X:-1 Y:79 Z:3
    -
        name: Nether
        coords: X:23 Y:65 Z:-19

Java:

Bukkit.broadcastMessage(ChatColor.YELLOW + getConfig().getString("path?") + ChatColor.WHITE + ", " + ChatColor.GREEN + getConfig().getString("path?"));

I am currently trying to figure out how to get a String from my config.yml into my Java code. More specifically, which path to use to get "Home", "X:-1 Y:79 Z:3", "Nether" or "X:23 Y:65 Z:-19" extracted into my Java code. I already tried getConfig().getString("Streuzel[0].name"), but that didn't work.

YAML: (Array with multiple objects)

Streuzel:
    -
        name: Home
        coords: X:-1 Y:79 Z:3
    -
        name: Nether
        coords: X:23 Y:65 Z:-19

Java:

Bukkit.broadcastMessage(ChatColor.YELLOW + getConfig().getString("path?") + ChatColor.WHITE + ", " + ChatColor.GREEN + getConfig().getString("path?"));

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

爱要勇敢去追 2025-01-17 04:39:43

实际上,Streuzel 是一个List,而不是数组,因此您需要使用getList(String) 而不是getString(String) 。就您而言,此列表的每个元素都是 Map。因此,如果您不关心类型转换安全性(因为 yaml 可以由人类编辑,并且可能会丢失结构),您可以使用如下内容:

((Map<String, String>)config.getList("Streuzel").get(0)).get("name") // Home

但理想情况下,您可以检查“Streuzel”是否是一个列表,并且如果它包含 Map,或尝试...捕获 ClassCastException。

关于 3 分钟前发布的答案:Streuzel 是一个 List,而不是 Map
编辑:他们修好了

Actually, Streuzel is a List, not an array, so you need to use getList(String) instead of getString(String). In your case, each element of this list is a Map<String, String>. So, if you don't care about type cast safety (because yaml can be edited by humans and it potentially can lose structure), you can use something like this:

((Map<String, String>)config.getList("Streuzel").get(0)).get("name") // Home

But ideally, you could either check if "Streuzel" is a list and if it contains a Map, or try...catch ClassCastException.

About an answer that got posted 3 minutes ago: Streuzel is a List, not a Map!
EDIT: they fixed it

爱情眠于流年 2025-01-17 04:39:43

如果您确实想获取列表,可以使用 getConfig().getList("Streuzel") 它将返回 LinkedHashMap 列表。您可以这样使用它:

LinkedHashMap<String, Object> myObj = (LinkedHashMap<String, Object>) getConfig().getList("Streuzel").get(0);
Bukkit.broadcastMessage(ChatColor.YELLOW + myObj.get("name") + ChatColor.WHITE + ", " + ChatColor.GREEN + myObj.get("coords"));

但是,不建议这样做。最好像这样使用配置节:

Streuzel:
    1:
        name: Home
        coords: X:-1 Y:79 Z:3
    2:
        name: Nether
        coords: X:23 Y:65 Z:-19

然后,您可以像这样使用它们:

Bukkit.broadcastMessage(ChatColor.YELLOW + getConfig().getString("Streuzel.1.name") + ChatColor.WHITE + ", " + ChatColor.GREEN + getConfig().getString("Streuzel.1.coords"));

这样,使用节,可以更轻松地获取所有内容。如果你想找到所有部分,请这样做:

// here is the config with all keys
ConfigurationSection globalSection = getConfig().getConfigurationSection("Streuzel");
for(String keys : globalSection.getKeys(false)) { // all kays: "1", "2" ...
   ConfigurationSection objSection = globalSection.getConfigurationSection(keys);
   // here you can load your object, log it or get content with objSection.getString()
}

If you really want to get as list, you can use getConfig().getList("Streuzel") that will return a list of LinkedHashMap<>. You can use it like that:

LinkedHashMap<String, Object> myObj = (LinkedHashMap<String, Object>) getConfig().getList("Streuzel").get(0);
Bukkit.broadcastMessage(ChatColor.YELLOW + myObj.get("name") + ChatColor.WHITE + ", " + ChatColor.GREEN + myObj.get("coords"));

But, this is not recommended. It's better to use configuration section like that:

Streuzel:
    1:
        name: Home
        coords: X:-1 Y:79 Z:3
    2:
        name: Nether
        coords: X:23 Y:65 Z:-19

Then, you can use them like that:

Bukkit.broadcastMessage(ChatColor.YELLOW + getConfig().getString("Streuzel.1.name") + ChatColor.WHITE + ", " + ChatColor.GREEN + getConfig().getString("Streuzel.1.coords"));

This way, with section, make it so much easier to get all content. If you want to find all sections, do like that:

// here is the config with all keys
ConfigurationSection globalSection = getConfig().getConfigurationSection("Streuzel");
for(String keys : globalSection.getKeys(false)) { // all kays: "1", "2" ...
   ConfigurationSection objSection = globalSection.getConfigurationSection(keys);
   // here you can load your object, log it or get content with objSection.getString()
}
滥情空心 2025-01-17 04:39:43

正如其他人所说,您可以获得 Map的列表 虽然这工作得

List<Map<String, Object>> list = (List<Map<String, Object>>) getConfig().getList("Streuzel");

很好,但我个人不喜欢它,希望它支持类似的东西:

List<ConfigurationSection> sections = getConfig().getSectionList("Streuzel");

但据我所知,Bukkit API 和 API 都不是基于它的(比如 Simple-YAML) ,支持这个。

我制作了一个支持获取部分列表的 API,请随意查看(它还支持 JSON 和 Yaml)。

As others have said, you can get a list of Map<String, Object> by using like so:

List<Map<String, Object>> list = (List<Map<String, Object>>) getConfig().getList("Streuzel");

Though this works just fine, I personally am not a fan of it and wish it would support something like:

List<ConfigurationSection> sections = getConfig().getSectionList("Streuzel");

But as far as I know, neither the Bukkit API nor API's based off of it (like Simple-YAML), support this.

I have made an API that does support getting a list of sections, feel free to check it out (it also supports both JSON and Yaml).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文