java - How wrap json array to custom object with collection via Gson? -
how wrap json array custom object collection containing array via gson? have following json string:
[ { "showid":410, "siteid":85, "name":"Майстер і маргарита", "duration":7200, "providerid":1016, "events":[ { "siteid":85, "eventsiteid":0, "providerid":1016, "eventid":1178, "hallid":0, "premiere":false, "origin":"20140912190000" } ] } ]
and want deserialize object bellow:
public class shows { private list<show> shows; public list<show> getshows() { return shows; } public void setshows(list<show> shows) { this.shows = shows; } }
this json
message represent list<show>
show
contains list
of events well.
this not json
of shows
, if should this.
{ "shows":[ { "showid":410, "siteid":85, "name":"Майстер і маргарита", "duration":7200, "providerid":1016, "events":[ { "siteid":85, "eventsiteid":0, "providerid":1016, "eventid":1178, "hallid":0, "premiere":false, "origin":"20140912190000" } ] } ] }
but can try way list<show>
, set shows
you can try way.
type collectiontype = new typetoken<list<show>>() { }.gettype(); string jsonstring="[\n" + "\n" + " {\n" + " \"showid\":410,\n" + " \"siteid\":85,\n" + " \"name\":\"Майстер і маргарита\",\n" + " \"duration\":7200,\n" + " \"providerid\":1016,\n" + " \"events\":[\n" + " {\n" + " \"siteid\":85,\n" + " \"eventsiteid\":0,\n" + " \"providerid\":1016,\n" + " \"eventid\":1178,\n" + " \"hallid\":0,\n" + " \"premiere\":false,\n" + " \"origin\":\"20140912190000\"\n" + " }\n" + " ]\n" + " }\n" + "\n" + "]"; list<show> showlist=new gson().fromjson(jsonstring,collectiontype); shows shows=new shows(); shows.setshows(showlist); system.out.println(shows);
my result.
shows{shows=[show{showid=410, siteid=85, name='Майстер і маргарита', duration=7200, providerid=1016, events=[events{siteid=85, eventsiteid=0, providerid=1016, eventid=1178, hallid=0, premiere=false, origin='20140912190000'}]}]}
my show
class.
public class show { private int showid; private int siteid; private string name; private int duration; private int providerid; private list<events> events; //getters , setters @override public string tostring() { return "show{" + "showid=" + showid + ", siteid=" + siteid + ", name='" + name + '\'' + ", duration=" + duration + ", providerid=" + providerid + ", events=" + events + '}'; } }
my events
class
public class events { private int siteid; private int eventsiteid; private int providerid; private int eventid; private int hallid; private boolean premiere; private string origin; // getters , setters @override public string tostring() { return "events{" + "siteid=" + siteid + ", eventsiteid=" + eventsiteid + ", providerid=" + providerid + ", eventid=" + eventid + ", hallid=" + hallid + ", premiere=" + premiere + ", origin='" + origin + '\'' + '}'; } }
Comments
Post a Comment