Expected Begin_Array But Was Begin_Object at Line 1 Column 2

Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2

You state in the comments that the returned JSON is this:

{ 
"dstOffset" : 3600,
"rawOffset" : 36000,
"status" : "OK",
"timeZoneId" : "Australia/Hobart",
"timeZoneName" : "Australian Eastern Daylight Time"
}

You're telling Gson that you have an array of Post objects:

List<Post> postsList = Arrays.asList(gson.fromJson(reader,
Post[].class));

You don't. The JSON represents exactly one Post object, and Gson is telling you that.

Change your code to be:

Post post = gson.fromJson(reader, Post.class);

Error in Kotlin (Retrofit) Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $ [SOLVED]

In your Retrofit service, you're saying you expect a List of items to come back, but your API is returning a single object with a list inside of it.

What you want to do instead is create a type called UserResults (or something similar) that contains a results property, which is a List<UsersItem>.

data class UserResults(
val results: List<UsersItem>
)

Then, your Retrofit function can return this new type instead:

@GET("api/users")
fun getUsers(): Call<UserResults>

RETROFIT Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $ KOTLIN

How to parse nested List with Retrofit and Gson?
Here is a similar question. Your models should be like

data class Movie(
val page: Int,
val pages: Int,
val total: String,
val tv_shows: List<TvShow>
)
data class TvShow(
val country: String,
val end_date: Any,
val id: Int,
val image_thumbnail_path: String,
val name: String,
val network: String,
val permalink: String,
val start_date: String,
val status: String
)

Then API class

interface MovieAPI {
@GET("api/most-popular?page=1")
suspend fun getData(): Response<Movie>//instead of Response<ArrayList<Movie>>

In the activity

 CoroutineScope(Dispatchers.IO).launch {
val response = retrofit.getData()

if (response.isSuccessful){
response.body()?.let {
shows = List(it.tv_shows)
}
}
for(show in shows){
println(show.name) // here are the names
}
}

If you want to use different property names for your models, you should annotate those property names with @SerializedName(). for more information please refer to Gson: @Expose vs @SerializedName

Expected BEGIN ARRAY but was BEGIN_OBJECT at line 1 and colum2

I have read completely about Retrofit, Retrofit will work when the array is serialized perfectly.

 "offersdata": [        {            "offercode": "GRAB20",            "title": "Get Upto",            "description": "20% off on selected merchandise on purchase of INR 1000 or more"        },        {            "offercode": "JAN20",            "title": "Get Upto",            "description": "20% Off on all purchases in January"        },        {            "offercode": "BHHH",            "title": "DES",            "description": "FDSFS"        }    ],    "message": "success"}


Related Topics



Leave a reply



Submit