英文:
deserialize list of strings into JSON in kotlin
问题
I'm trying to deserialize the following string into JSON:
{
"user_id": "id1",
"old_user_ids": ["id1", "id2", "id3"],
"status": "ACTIVE"
}
I tried to deserialize using:
val record = objectMapper.readValue(myList, UserRecord::class.java)
My data class looks like this:
data class UserRecord(
var user_id: String = "",
var old_user_ids: List<String> = emptyList(),
var status: String = ""
)
However, I'm getting the following error:
Cannot construct instance of `java.util.ArrayList` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('[') at [Source: (String)...
I would appreciate if someone can point me to what I'm having wrong.
英文:
I'm trying to deserialize the following string into JSON
val myList ="""{"user_id": "id1" ,"old_user_ids": "["id1,id2,id3"]", "status":"ACTIVE"}"""
I tried to deserialize using
val record = objectMapper.readValue(myList, UserRecord::class.java)
my data class looks like this
data class UserRecord(
var user_id: String = "",
var old_user_ids: List<String> = emptyList(),
var status: String = ""
)
however, I'm getting the following error
Cannot construct instance of `java.util.ArrayList` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('[') at [Source: (String)"{"user_id": "id1" ,"old_user_ids": "["id1,id2,id3"]", "status":"ACTIVE"}";
I would appreciate if someone can point me to what I'm having wrong.
Output I'm trying to get is
{
"user_id": "id1",
"old_user_ids": ["id1","id2","id3"],
"status":"ACTIVE"
}
答案1
得分: 1
你的JSON格式不正确,应该是:
{"user_id": "id1", "old_user_ids": ["id1", "id2", "id3"], "status": "ACTIVE"}
你在数组的 [
和 ]
分隔符周围使用了引号,这是导致问题的原因。此外,数组中的每个项都需要用引号括起来。
英文:
Your JSON is invalid, it should be
{"user_id": "id1" ,"old_user_ids": ["id1","id2","id3"], "status":"ACTIVE"}
You have quotes around the [
]
array delimiters which is why it's choking on that first character. Also your items in the array need quotes around each.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论