我有Yaml文件:
#Define CDN domains
---
CDN:
quality: 200..300
cost: low
Video-type: mp4使用这个Java代码,我检索CDN的子值:
// The path of your YAML file.
Yaml yaml = new Yaml();
Map<String, Map<String, String>> values =
(Map<String, Map<String, String>>) yaml
.load(new FileInputStream(new File("/workspace/servlet-yaml/src/test.yaml")));
for (String key : values.keySet()) {
Map<String, String> subValues = values.get(key);
for (String subValueKey : subValues.keySet()) {
System.out.println(values);
}
}产出如下:
{CDN={quality=200..300, cost=low, Video-type=mp4}}
{CDN={quality=200..300, cost=low, Video-type=mp4}}
{CDN={quality=200..300, cost=low, Video-type=mp4}}if cost = low代码,然后做一些事情。发布于 2016-11-16 08:03:26
首先,我不知道它为什么收获三次?
因为你是这么说的。对于每个subValueKey,打印整个值集。有三个子键,所以完整的值集会被打印三次.
其次,我想编写一个代码,如果成本=低,那么就做一些事情。
Yaml yaml = new Yaml();
Map<String, Map<String, String>> values =
(Map<String, Map<String, String>>) yaml.load(
new FileInputStream(new File(
"/workspace/servlet-yaml/src/test.yaml")));
final Map<String, String> cdn = values.get("CDN");
// or iterate over all keys like you currently do
final String cost = cdn.get("cost");
// or iterate over all subkeys and compare them to "cost".
// that way, it's easier to handle missing keys.
if ("low".equals(cost)) {
// do something
}https://stackoverflow.com/questions/40618934
复制相似问题