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