---
university: scsb
country: us
Entities:
!Entity
name: john
subjects:
-math
-English
-C++
!Entity
name: mary
subjects:
-science
-French
我正在尝试将上述文件加载到地图中,实体部分下的数据将映射到实体对象的集合。这是一个正确的yaml语法,因为我得到了yaml解析器错误。
答案 0 :(得分:0)
我对以下内容感到非常幸运:
---
university: scsb
country: us
Entities: {
!Entity {
name: john,
subjects:
-math
-English
-C++
},
!Entity {
name: mary,
subjects:
-science
-French
}
}
以下是使用SnakeYAML自定义标记(!dice)的示例。完整的示例是here。它来自SnakeYAML documentation。
class DiceConstructor extends SafeConstructor {
public DiceConstructor() {
this.yamlConstructors.put(new Tag("!dice"), new ConstructDice());
}
private class ConstructDice extends AbstractConstruct {
public Object construct(Node node) {
String val = (String) constructScalar((ScalarNode) node);
int position = val.indexOf('d');
Integer a = new Integer(val.substring(0, position));
Integer b = new Integer(val.substring(position + 1));
return new Dice(a, b);
}
}
}
@SuppressWarnings("unchecked")
public void testConstructor() {
Yaml yaml = new Yaml(new DiceConstructor());
Object data = yaml.load("{initial hit points: !dice '8d4'}");
Map<String, Dice> map = (Map<String, Dice>) data;
assertEquals(new Dice(8, 4), map.get("initial hit points"));
}