我正在建模一些 JSON - 并使用以下几行
data class Metadata(
val id: String,
val creators: Array<CreatorsModel>
)
随着:
data class CreatorsModel (
val role: String,
val name: String
)
但是继续看到错误:
Array property in data class error
.任何想法为什么会这样?
仅供引用,JSON 看起来像:
{
"id": "123",
"creators": [{
"role": "Author",
"name": "Marie"
}
]
}
最佳答案
在 Kotlin 中,您应该旨在使用 List
而不是 Array
在可能的情况。 Array
对 JVM 有一些影响,尽管编译器允许,但 IDE 可能会提示您覆盖 equals
和 hashcode
手动。使用 List
会让事情变得简单得多。
您可以在此处了解有关差异的更多信息:Difference between List and Array types in Kotlin
关于json - Kotlin - 数据类错误中的数组属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57249356/