0

i'm trying to parse this JSON string that returns the ids of movies from this api "https://rapidapi.com/apidojo/api/online-movie-database/" but i get a error

[
    "/title/tt1649418/",
    "/title/tt10954984/",
    "/title/tt9114286/",
    "/title/tt10648342/",
    "/title/tt4471908/"
]

into this object

data class PopularMovies(
    val popularMovies: ArrayList<String>
)

but it throws me with

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2

please anyone can help me?

1 Answers1

0

You may use TypeToken to load the json string into a custom object.

Here is the Kotlin example code in your case:

val jsonString = "[\n" +
        "    \"/title/tt1649418/\",\n" +
        "    \"/title/tt10954984/\",\n" +
        "    \"/title/tt9114286/\",\n" +
        "    \"/title/tt10648342/\",\n" +
        "    \"/title/tt4471908/\"\n" +
        "]"

val typeToken = object : TypeToken<ArrayList<String>>() {}.type
popularMovies = Gson().fromJson<ArrayList<String>>(jsonString, typeToken)
Log.d(TAG, "First string:" + popularMovies[0])

Or you can see this answer for various alternatives.

Desmond
  • 203
  • 1
  • 9