【发布时间】:2022-08-02 18:23:42
【问题描述】:
我很难弄清楚如何在 Kotlin 中读取 YAML 文件。
简而言之,YAML 具有以下格式:
aws:
foo:
dev:
id: \'1111\'
pro:
id: \'2222\'
bar:
dev:
id: \'3333\'
pro:
id: \'4444\'
我创建了这些数据类:
data class Account (
val id: String
)
data class Owner (
val accounts: List<Account>
)
data class Cloud (
val owners: List<Owner>
)
然后我尝试解析文件:
val mapper = ObjectMapper().registerModule(KotlinModule())
val settings: Cloud = mapper.readValue(Path.of(\"accounts.yaml\").toFile())
# also tried this
val settings: List<Cloud> = mapper.readValue(Path.of(\"accounts.yaml\").toFile())
println(settings)
println 以 Exception in thread \"main\" com.fasterxml.jackson.core.JsonParseException: Unrecognized token \'aws\': was expecting (JSON String, Number, Array, Object or token \'null\', \'true\' or \'false\') 失败
为什么?
-
YAML 和 JSON 不一样
-
@IvoBeckers 当然不是。刚刚修复了问题文本中不正确的
val mapper行