Reputation: 839
i'have json array like below :
{
"otg": [
{
"id": "1",
"name": "Forum OTG Nasional",
"description": "otg description",
"banner": "",
"date": "June, 18th 2015",
"time": "08:06"
}
]
}
and i want to retrieve this json using models using gson, this is my models class :
import java.util.ArrayList;
import java.util.List;
import com.google.gson.annotations.Expose;
public class ModelB {
@Expose
private List<Otg> otg = new ArrayList<Otg>();
public List<Otg> getOtg() {
return otg;
}
public void setOtg(List<Otg> otg) {
this.otg = otg;
}
public class Otg {
@Expose
private String id;
@Expose
private String name;
@Expose
private String description;
@Expose
private String banner;
@Expose
private String date;
@Expose
private String time;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getBanner() {
return banner;
}
public void setBanner(String banner) {
this.banner = banner;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
}
}
then below my code to retrieve json using model :
@Override
public void updateModel(String models) {
list_model = new ArrayList<ModelB>();
try {
List<ModelB> model = new Gson().fromJson(models, new TypeToken<List<ModelB>>() {
}.getType()); // CANNOT READ THIS LINE
Log.d("COUNT_check", model.size() + "");
list_model = model;
} catch (Exception e) {
e.printStackTrace();
Log.d("check_error", "error_home"); // ALWAYS DISPLAY IT
}
}
but my code can't retrieve in model list, always error. what solutions about the problem ?
Upvotes: 0
Views: 791
Reputation: 19243
maybe try smth like this:
public class Clazz{
public static class List extends ArrayList<Clazz> {}
@SerializedName("custom_parameter")
private String customParameter;
public final String getCustomParameter(){
return customParameter;
}
public final Clazz setCustomParameter(final String customParameter){
this.customParameter = customParameter;
return this;
}
}
obtaining:
Clazz.List clazzList = new Gson().fromJson(jsonString, Clazz.List.class);
Upvotes: 0
Reputation: 1526
If you want to retrieve list of Otg objects, then you can do the following:
@Override
public void updateModel(String models) {
...
List<Otg> otgs = new Gson().fromJson(models.getAsJsonObject().get("otg"),
new TypeToken<List<Otg>>() {
}.getType());
...
}
Upvotes: 0
Reputation: 1704
Remove this
@Expose
private List<Otg> otg = new ArrayList<Otg>();
public List<Otg> getOtg() {
return otg;
}
public void setOtg(List<Otg> otg) {
this.otg = otg;
}
Only
public class ModelB{
@Expose
private String id;
@Expose
private String name;
@Expose
private String description;
@Expose
private String banner;
@Expose
private String date;
@Expose
private String time;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getBanner() {
return banner;
}
public void setBanner(String banner) {
this.banner = banner;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
}
Then it will give you expected array of list. Otherwise if you want same model which you have then it will parse just simple as:
ModelB gsonObj = gson.fromJson(jsonString, ModelB.class);
then you can get your arraylist
ArrayList<ModelB> yourList= gsonObj.getOtg();
Upvotes: 0
Reputation: 93842
i'have json array like below :
This is not an array, it's an object which has a key-value mapping with maps a key to an array (which contains zero or more Otg
instances or whatever you called it).
Hence your datas does not represent a List<ModelB>
, but a single ModelB
instance:
ModelB model = new Gson().fromJson(s, ModelB.class);
This is what the error message indicated:
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $
at com.google.gson.Gson.fromJson(Gson.java:822)
at com.google.gson.Gson.fromJson(Gson.java:775)
at com.google.gson.Gson.fromJson(Gson.java:724)
By deserializing it into a List<ModelB>
, you told the parser that the JSON data should start with an array ([...]
) while in fact it starts with an object ({...}
).
Upvotes: 3