Search code examples
jsonkotlinserialization

How do I properly serialize a JSON file that contains an array of strings


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]


Solution

  • 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