解析yaml文件 [英] Parse a yaml file
问题描述
这是我第一次使用 yaml
文件,所以我首先想到的是找到任何可以帮助我解析文件的库。
This is the first time I am working with yaml
files, so the first think I looked at was to find any library that could help me to parse the file.
我找到两个库 YamlBean
和 SnakeYaml
我是不知道我将使用哪一个。
I have find two library YamlBean
and SnakeYaml
I am not sure which one that I am going to use.
以下是我要解析的文件示例
Here is an example of the file that I am trying to parse
users:
user1:
groups:
- Premium
user2:
groups:
- Mod
user3:
groups:
- default
groups:
Mod:
permissions:
test: true
inheritance:
- Premium
default:
permissions:
test.test: true
inheritance:
- Mod
Admin:
permissions:
test.test.test: true
inheritance:
- Mod
该文件将发生动态变化,因此我不知道该文件将连续多少个用户或组ain。
The file will change dynamical so I don't know how many users or groups the file would contain.
我想从中获取的信息是用户名和这样的组
The information I would like to fetch from this is the user name and the group like this
user1 Premium
user2 Mod
user3 default
并且从组中只有组名,比如这个
and from the groups only the group names, like this
Mod
default
Admin
任何人都可以让我从这里开始?什么是最好用的库?
YamlBean或SnakeYaml。
Anyone could get me started here? and what is the best library to use for this? YamlBean or SnakeYaml.
我想,我需要将信息保存在我很容易迭代的内容中。
I guess, I need to save the information in something that I easily could iterate over.
推荐答案
我最终使用了SnakeYaml并制作了一些拆分字符串来解决我的问题。
I ended up using SnakeYaml and made some split strings to solve my issue.
将yaml文件加载到 Object
然后加载到 Map
,然后将结果从将
映射到 String []
然后在中用于
循环I从 String []
中读出名称。我对团队做了同样的事情。
Loaded the yaml file to Object
and then into a Map
, then split the result from the Map
into String[]
and then in a for
loop I read out the name from the String[]
. I did the same with groups.
我知道有更好的解决方案,但这对于这个项目已经足够了。
I know that there is better solutions out there but this is good enough for this project.
感谢所有回复。
这篇关于解析yaml文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!