我很難弄清楚如何在 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')
為什么?
uj5u.com熱心網友回復:
您需要包含jackson-dataformat-yaml依賴項,然后像這樣創建您的 ObjectMapper:
val mapper = ObjectMapper(YAMLFactory()).registerModule(KotlinModule())
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/454181.html
下一篇:如何控制手機中的其他應用程式
