json - how create model class using gson of JsonArray where array is variable not constant android -
{ "801345": [{ "type": "football", "name": "a", "id": "1" }, { "type": "football", "name": "b", "id": "2" },{ "type": "cricket", "name": "c", "id": "3" }, { "type": "volley", "name": "d", "id": "4" }]
}
{ "910358": [{ "type": "football", "name": "a", "id": "1" }, { "type": "football", "name": "b", "id": "2" },{ "type": "cricket", "name": "c", "id": "3" }, { "type": "volley", "name": "d", "id": "4" }]
}
here team array not constant variable. team selected dropdown , different id different team. how create model class using variable array. team id different here 910358,801345
use this site creating pojo class http://www.jsonschema2pojo.org/
updates
i created method parsing type json. try this
private arraylist<teams> teamsjsonparsing(string json) throws jsonexception { /*json="{\n" + "\"801345\": [{\n" + " \"type\": \"football\",\n" + " \"name\": \"a\",\n" + " \"id\": \"1\"\n" + "}, {\n" + " \"type\": \"football\",\n" + " \"name\": \"b\",\n" + " \"id\": \"2\"\n" + "},{\n" + " \"type\": \"cricket\",\n" + " \"name\": \"c\",\n" + " \"id\": \"3\"\n" + "}, {\n" + " \"type\": \"volley\",\n" + " \"name\": \"d\",\n" + " \"id\": \"4\"\n" + "}]\n" + ",\n" + "\n" + "\n" + "\"910358\": [{\n" + " \"type\": \"football\",\n" + " \"name\": \"a\",\n" + " \"id\": \"1\"\n" + "}, {\n" + " \"type\": \"football\",\n" + " \"name\": \"b\",\n" + " \"id\": \"2\"\n" + "},{\n" + " \"type\": \"cricket\",\n" + " \"name\": \"c\",\n" + " \"id\": \"3\"\n" + "}, {\n" + " \"type\": \"volley\",\n" + " \"name\": \"d\",\n" + " \"id\": \"4\"\n" + "}]\n" + "}"; */ jsonobject jsonobject=new jsonobject(json); iterator itr =jsonobject.keys(); gson gson=new gson(); arraylist<teams> teamsarraylist=new arraylist<>(); while(itr.hasnext()) { object element = itr.next(); log.e("iterator",jsonobject.getjsonarray(element.tostring()).tostring()); teams teams=new teams(); teams.setteamname(element.tostring()); arraylist<team> teamarraylist=new arraylist<>(); jsonarray jsonarray=jsonobject.getjsonarray(element.tostring()); (int i=0;i<jsonarray.length();i++){ team team=gson.fromjson(jsonarray.getjsonobject(i).tostring(),team.class); teamarraylist.add(team); } teams.setteam(teamarraylist); teamsarraylist.add(teams); } return teamsarraylist; }
example usage
try { arraylist<teams> teamses= teamsjsonparsing(json); log.e("team",teamses.size()+""); } catch (jsonexception e) { e.printstacktrace(); }
your model classes are
public class team { @serializedname("type") @expose private string type; @serializedname("name") @expose private string name; @serializedname("id") @expose private string id; public string gettype() { return type; } public void settype(string type) { this.type = type; } public string getname() { return name; } public void setname(string name) { this.name = name; } public string getid() { return id; } public void setid(string id) { this.id = id; } } public class teams { string teamname ; public string getteamname() { return teamname; } public void setteamname(string teamname) { this.teamname = teamname; } private list<team> team = null; public list<team> getteam() { return team; } public void setteam(list<team> team) { this.team = team; } }
Comments
Post a Comment