Gson Expected Begin_Array But Was Begin_Object

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);

Expected BEGIN_ARRAY but was BEGIN_OBJECT when using GSON

JSON Objects are enclosed directly in curly brackets {} vs. JSON Arrays that are enclosed in square brackets [] inside JSON Objects.

The classes Purchases and PlayerRank should be defined in this way:

public class Purchases{
@SerializedName("buyers") protected ArrayList<PlayerRank> buyers;

...
}

public class PlayerRank{
@SerializedName("IGN") protected String ign;
@SerializedName("ProductID") protected int productId;

...
}

Note the SerializedName notation that lets you decouple the name of the objects/arrays in the json file from the names of your java properties.

The protected I added to the properties just makes it explicit what the original classes defaulted to in the original code.

The JSON file should be something like this:

{
"buyers" : [
{ "IGN": "MGlolenstine", "ProductID": "51"},
{ "IGN": "MGlolenstine", "ProductID": "55"},
...
{ "IGN": "MGlolenstine", "ProductID": "56"}
]
}

And to read the JSON into a variable:

Purchases p = gson.fromJson(new FileReader(path), Purchases.class);

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

Since your JSON is not a JSON array, but rather a JSON object containing an array, you would need to write a class that contains the ArrayList:

public class EatResponse {
@SerializedName("eat")
private ArrayList<Eat> eatList;

public ArrayList<Eat> getEatList() {
return eatList;
}
}

Then, you just need to parse that from your JSON with a call that would look something like this:

EatResponse response = gson.fromJson(json, EatResponse.class);
ArrayList<Eat> eatList = response.getEatList();

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"}

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

GSON Expected BEGIN_ARRAY but was BEGIN_OBJECT?

I found a simply solution for solve my problem. I will show here my solution, maybe this will help some people.

So I created two new class : HabilitationAD and HabilitationInterprete

public class HabilitationAD {

@SerializedName("Key")
private String mKey;

@SerializedName("Value")
private List<BeanHabilitation> mValue;

public String getmKey() {
return mKey;
}

public void setmKey(String mKey) {
this.mKey = mKey;
}

public List<BeanHabilitation> getmValue() {
return mValue;
}

public void setmValue(List<BeanHabilitation> mValue) {
this.mValue = mValue;
}

public HabilitationAD(){

}

public HabilitationAD(String p_mKey, List<BeanHabilitation> p_mValue){
this.mKey = p_mKey;
this.mValue = p_mValue;
}
}

and :

    public class HabilitationInterprete {

@SerializedName("Key")
private String mKey;

@SerializedName("Value")
private List<BeanHabilitation> mValue;

public String getmKey() {
return mKey;
}

public void setmKey(String mKey) {
this.mKey = mKey;
}

public List<BeanHabilitation> getmValue() {
return mValue;
}

public void setmValue(List<BeanHabilitation> mValue) {
this.mValue = mValue;
}

public HabilitationInterprete(){

}

public HabilitationInterprete(String p_mKey, List<BeanHabilitation> p_mValue){
this.mKey = p_mKey;
this.mValue = p_mValue;
}

And I modify my class InfoSecuriteBean

I changed this :

 @SerializedName("HabilitationAD")
private Map<String,List<HabilitationBean>> mHabilitationAD = new HashMap<String, List<HabilitationBean>>();
@SerializedName("HabilitationInterprete")
private Map<String,List<HabilitationBean>> mHabilitationInterprete = new HashMap<String, List<HabilitationBean>>();

by this :

@SerializedName("HabilitationAD")
private List<HabilitationAD> habilitationAD;
@SerializedName("HabilitationInterprete")
private List<HabilitationInterprete> habilitationInterprete;

Now all work great ! Hope this solution will help !

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 148 path $.main

The data class you are generating for your JSON response is not correct. Many of the things are objects, but you have assigned it as a List item. Here is the correct data class response based on your JSON. So the JSON response is not being parsed properly.

data class CurrentWeatherResponse(
val base: String,
val clouds: Clouds,
val cod: Int,
val coord: Coord,
val dt: Int,
val id: Int,
val main: Main,
val name: String,
val sys: Sys,
val timezone: Int,
val visibility: Int,
val weather: List<Weather>,
val wind: Wind
)

data class Clouds(
val all: Int
)

data class Coord(
val lat: Double,
val lon: Double
)

data class Main(
val feels_like: Double,
val humidity: Int,
val pressure: Int,
val temp: Double,
val temp_max: Double,
val temp_min: Double
)

data class Sys(
val country: String,
val id: Int,
val sunrise: Int,
val sunset: Int,
val type: Int
)

data class Weather(
val description: String,
val icon: String,
val id: Int,
val main: String
)

data class Wind(
val deg: Int,
val gust: Double,
val speed: Double
)

I suggest you try to use this and try again. Here is also a plugin that automatically generates data classes based of JSON. It might help you more.

https://plugins.jetbrains.com/plugin/10054-generate-kotlin-data-classes-from-json

Expected BEGIN_OBJECT but was BEGIN_ARRAY but the json respone has already an object

You are using the wrong model. according to your json output, your model must be the below classes

public class Output{

public Status status;
public List<RiceField> riceField;

}

public class RiceField {

public Integer id;
public String title;
public Integer harga;
public Integer luas;
public String alamat;
public String maps;
public String deskripsi;
public String sertifikasi;
public String tipe;
public String createdAt;
public String updatedAt;
public Integer userId;
public Integer vestigeId;
public Integer irrigationId;
public Integer regionId;
public Integer verificationId;

}

public class Status {

public Integer code;
public String message;
public String description;

}

for more information use this website :
json to java class



Related Topics



Leave a reply



Submit