科特林 |杰克逊注解 |如何修复 Out of START_ARRAY 令牌错误
Kotlin | Jackson annotation | How to fix Out of START_ARRAY token Error
谁能告诉我哪里做错了。我有 json 这样的
[
{
"id": "1",
"name": "ff",
"surname": "ggg",
"cap": "10000"
},
{
"id": "1",
"name": "aaa",
"surname": "hhh",
"cap": "22222"
},
{
"id": "1",
"name": "rrr",
"surname": "hhhhhdr",
"cap": "33333"
},
{
"id": "1",
"name": "hhh",
"surname": "qqqqq",
"cap": "44444"
}
]
然后我解析到这个 class。
data class ResponseList(
val capList: List<Response>?
) {
data class Response(
@JsonProperty("id")
val id: String,
@JsonProperty("name")
val name: String,
@JsonProperty("surname")
val surname: String,
@JsonProperty("cap")
val cap: String
)
}
当我尝试解析它时,列表总是空的,如果我尝试测试它,我会遇到这个错误:
无法从数组值(标记 JsonToken.START_ARRAY
)
中反序列化 com.myapp.ResponseList 类型的值
只需要 class 回复,如下所示:
fun test(){
val jsonStr = "your json str"
val mapper = ObjectMapper()
val lendReco: List<Response> =
mapper.readValue(jsonStr, object : TypeReference<List<Response?>?>() {})
}
data class Response(
@JsonProperty("id")
val id: String,
@JsonProperty("name")
val name: String,
@JsonProperty("surname")
val surname: String,
@JsonProperty("cap")
val cap: String
)
谁能告诉我哪里做错了。我有 json 这样的
[
{
"id": "1",
"name": "ff",
"surname": "ggg",
"cap": "10000"
},
{
"id": "1",
"name": "aaa",
"surname": "hhh",
"cap": "22222"
},
{
"id": "1",
"name": "rrr",
"surname": "hhhhhdr",
"cap": "33333"
},
{
"id": "1",
"name": "hhh",
"surname": "qqqqq",
"cap": "44444"
}
]
然后我解析到这个 class。
data class ResponseList(
val capList: List<Response>?
) {
data class Response(
@JsonProperty("id")
val id: String,
@JsonProperty("name")
val name: String,
@JsonProperty("surname")
val surname: String,
@JsonProperty("cap")
val cap: String
)
}
当我尝试解析它时,列表总是空的,如果我尝试测试它,我会遇到这个错误:
无法从数组值(标记 JsonToken.START_ARRAY
)
只需要 class 回复,如下所示:
fun test(){
val jsonStr = "your json str"
val mapper = ObjectMapper()
val lendReco: List<Response> =
mapper.readValue(jsonStr, object : TypeReference<List<Response?>?>() {})
}
data class Response(
@JsonProperty("id")
val id: String,
@JsonProperty("name")
val name: String,
@JsonProperty("surname")
val surname: String,
@JsonProperty("cap")
val cap: String
)