对于具有多个对象的 YAML 数组,在 Java 中使用什么路径?
我目前正在尝试弄清楚如何将字符串从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
实际上,Streuzel 是一个List,而不是数组,因此您需要使用getList(String) 而不是getString(String) 。就您而言,此列表的每个元素都是
Map
。因此,如果您不关心类型转换安全性(因为 yaml 可以由人类编辑,并且可能会丢失结构),您可以使用如下内容:但理想情况下,您可以检查“Streuzel”是否是一个列表,并且如果它包含 Map,或尝试...捕获 ClassCastException。
关于 3 分钟前发布的答案:Streuzel
是一个List
,而不是Map
!编辑:他们修好了
Actually,
Streuzel
is aList
, not an array, so you need to usegetList(String)
instead ofgetString(String)
. In your case, each element of this list is aMap<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: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 aList
, not aMap
!EDIT: they fixed it
如果您确实想获取列表,可以使用
getConfig().getList("Streuzel")
它将返回LinkedHashMap
列表。您可以这样使用它:但是,不建议这样做。最好像这样使用配置节:
然后,您可以像这样使用它们:
这样,使用节,可以更轻松地获取所有内容。如果你想找到所有部分,请这样做:
If you really want to get as list, you can use
getConfig().getList("Streuzel")
that will return a list ofLinkedHashMap<>
. You can use it like that:But, this is not recommended. It's better to use configuration section like that:
Then, you can use them like that:
This way, with section, make it so much easier to get all content. If you want to find all sections, do like that:
正如其他人所说,您可以获得 Map的列表 虽然这工作得
很好,但我个人不喜欢它,希望它支持类似的东西:
但据我所知,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:
Though this works just fine, I personally am not a fan of it and wish it would support something like:
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).