英文:
How do I properly serialize a JSON file that contains an array of strings
问题
我有这个字符串数组:
["https://cdn.shibe.online/shibes/3f47b1c848763f156778015a20da91b5f365ea93.jpg","https://cdn.shibe.online/shibes/22ee83228be55bce16bc8c6e91790700229892b9.jpg"]
我尝试这样做,但它不起作用。
@Serializable
data class ShibePhoto(
@SerialName("photos")
val photos: List<String>
)
我得到这个错误:
kotlinx.serialization.json.internal.JsonDecodingException: 偏移0处的意外JSON令牌:预期对象的开头'{',但实际是'[',路径:$[0]
英文:
I have this array of strings:
["https://cdn.shibe.online/shibes/3f47b1c848763f156778015a20da91b5f365ea93.jpg","https://cdn.shibe.online/shibes/22ee83228be55bce16bc8c6e91790700229892b9.jpg"]
I'm trying to do this, but it's not working.
@Serializable
data class ShibePhoto(
@SerialName("photos")
val photos: List<String>
)
I'm getting this error
kotlinx.serialization.json.internal.JsonDecodingException: Unexpected JSON token at offset 0: Expected start of the object '{', but had '[' instead at path: $[0]
答案1
得分: 0
你基本上是在说响应的格式是这样的
{"photos":[...]}
而不仅仅是
[...]
你需要做的是甚至不要使用 ShibePhoto
类,而是在代码中使用 List<String>
,以取代你之前使用 ShibePhoto
的地方。
英文:
You are basically saying that the response is of the form
{"photos":[...]}
Instead of just
[...]
What you need to do is not even have the class ShibePhoto
and use List<String>
directly wherever you had ShibePhoto
in your code
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论