Whenever I am trying to load my gson.fromJsoncommand, it always throws an exception. The error is
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 7 column 2 path
The JSON Result that I am sending is this:
{"status":true,"statuscode":200,"message":"List(s) found!","result":[{"id":"1","name":"Afghan (AFGHANISTAN)","country_code":"AF"},{"id":"2","name":"Albanian (ALBANIA)","country_code":"AL"}],"batch":2,"totalrows":2}
and this is the TestModel:
package com.app.testapp.test.models;
import com.activeandroid.Model;
import com.activeandroid.annotation.Column;
import com.google.gson.annotations.SerializedName;
public class TestModel extends Model {
@Column(name="id")
@SerializedName("id")
String id;
@Column(name="name")
@SerializedName("name")
String name;
@Column(name="country_code")
@SerializedName("country_code")
String country_code;
public String getTestModelId(){ return id;}
public void setTestModelId(String TestModelId){
this.id = TestModelId;
}
public String getTestModelName(){ return ("name")
public void setTestModelName(String TestModelName){
this.name = TestModelName;
}
public String TestModelCode(){ return country_code;}
public void SetTestModelCode(String TestModelCode){
this.country_code = TestModelCode;
}
@Override
public String toString(){
return "Nationality [NationalityId="+id+", NationalityName="+name+" , " +
"NationalityCode="+country_code+"]";
}
}
and this is on my httpclient class, all seems to be working well until the error part
Gson gson = new GsonBuilder().create();
Type listType = new TypeToken<List<TestModel>>() {
}.getType();
try {
Log.v("MyTag","Try");
List<TestModel> result = gson.fromJson(new String(responseBody), listType); -- error here
callback.onGetTestSucess(result);
} catch (Exception e) {
callback.onGetTestFailure();
What seems to be the problem with the GSON? Is it because I'm not serializing it properly?
listType), when in fact the json you are parsing is not a list, it is an object. - 1615903{"status":true, ... }is not a json array... - Selvin