how to retrieve json using models in gson android? -
i'have json array below :
{ "otg": [ { "id": "1", "name": "forum otg nasional", "description": "otg description", "banner": "", "date": "june, 18th 2015", "time": "08:06" } ] }
and want retrieve json using models using gson, 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 code 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 line log.d("count_check", model.size() + ""); list_model = model; } catch (exception e) { e.printstacktrace(); log.d("check_error", "error_home"); // display } }
but code can't retrieve in model list, error. solutions problem ?
remove
@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 give expected array of list. otherwise if want same model have parse simple as:
modelb gsonobj = gson.fromjson(jsonstring, modelb.class);
then can arraylist
arraylist<modelb> yourlist= gsonobj.getotg();
Comments
Post a Comment