GSON throwing Expected BEGIN_OBJECT but was BEGIN_ARRAY?
The problem is you're telling Gson
you have an object of your type. You don't. You have an array of objects of your type. You can't just try and cast the result like that and expect it to magically work ;)
The User guide for Gson
Explains how to deal with this:
https://github.com/google/gson/blob/master/UserGuide.md
This will work:
ChannelSearchEnum[] enums = gson.fromJson(yourJson, ChannelSearchEnum[].class);
But this is better:
Type collectionType = new TypeToken<Collection<ChannelSearchEnum>>(){}.getType();
Collection<ChannelSearchEnum> enums = gson.fromJson(yourJson, collectionType);
gson.fromJson Expected BEGIN_OBJECT but was BEGIN_ARRAY
Looks like the JSON string (rankJSON
) is an array of JSON documents not a single JSON document.
If you log that JSON you'll see that it starts with [
e.g.
[
{
...
}
]
You are attempting to deserialize it into a single RankAPI
, you should instead deserialize it into a List<RankAPI>
, for example;
List<RankAPI> r = gson.fromJson(rankJSON, new TypeToken<ArrayList<RankAPI>>(){}.getType());
Here's a test case to verify this behaviour:
@Test
public void twoWayTransform() {
Gson gson = new GsonBuilder().serializeNulls().create();
List<RankAPI> incomings = Arrays.asList(new RankAPI(), new RankAPI());
String json = gson.toJson(incomings);
// use TypeToken to inform Gson about the type of the elements in the generic list
List<RankAPI> fromJson = gson.fromJson(json, new TypeToken<ArrayList<RankAPI>>(){}.getType());
assertEquals(2, fromJson.size());
for (RankAPI incoming : incomings) {
// this will pass if RankAPI has an equals() method
assertTrue(fromJson.contains(incoming));
}
}
Gson issue:- Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1
Use the second case, but replace
private SubTasks subTasks ;
with
private List<SubTasks> subTasks ;
The clue was in the error.
java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 276 path $.subTasks
Given your java classes, it was expecting an object named subTasks but found an array.
So change it to an array and you are gold.
The first case is probably correct, if you end up parsing an array of SMTStatus
OkHttp / Retrofit / Gson: Expected BEGIN_OBJECT but was BEGIN_ARRAY
Error: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $
As the error clearly says, you are trying to parse JSONArray into a JSONObject
@GET("characters/house/{house}")
Call<Data> getPersonajes(@Path("house") String house);
This service method expecting JSONObject, but according the logcat shared by in the image, the response is giving JSONArray. Instead you should parse it as:
@GET("characters/house/{house}")
Call<List<Personaje>> getPersonajes(@Path("house") String house)
gson.fromJson Expected BEGIN_OBJECT but was BEGIN_ARRAY because of how the object is stored
I just added [ ] before and after my JSON object in the exec sql statement + took the advice of the comment above and it works..for now
the code became thus:
db.execSQL("insert into favorites (obj) values ('["+g.toJson(items.get(getAdapterPosition()),Hotel.class).toString()+"]') ;");
//and when retrieved:
Gson gson = new Gson();
Type hotelListType = new TypeToken<ArrayList<Hotel>>(){}.getType();
ArrayList<Hotel> h = gson.fromJson(json, hotelListType );
this works
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
How to Fix 'Android.Os.Networkonmainthread
exception'
Firebaselistadapter Not Pushing Individual Items For Chat App - Firebase-Ui 3.1
Passing Data Through Intent Using Serializable
Java Can't Connect to X11 Window Server Using 'Localhost:10.0' as the Value of the Display Variable
How to Fix Java.Lang.Unsupportedclassversionerror: Unsupported Major.Minor Version
How to Create a File and Write to It
What Is the Point of the Diamond Operator (≪≫) in Java
How to Concatenate Two Arrays in Java
Recommendations For a Heap Analysis Tool For Java
Method Overloading For Null Argument
How to Return Datasnapshot Value as a Result of a Method
Android 6.0 Multiple Permissions
Android - Start Service on Boot
How to Generate Random Integers Within a Specific Range in Java