Reputation: 221
{
"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 is not constant it is variable. team is selected from dropdown and different Id for different team. How to create model class using variable array. team id are different like here 910358,801345
Upvotes: 1
Views: 1095
Reputation: 4510
Use this site for creating POJO class http://www.jsonschema2pojo.org/
Updates
I created a method for parsing this 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());
for (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;
}
}
Upvotes: 1
Reputation: 2815
Your model class must be like this...
public class TeamListResponse {
private List<Team> team;
public List<Team> getTeam() {
return team;
}
public void setTeam(List<Team> team) {
this.team = team;
}
private class Team{
private String type,name,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;
}
}
}
Upvotes: 0
Reputation: 756
Your Model Class Will be
@SerializedName("team")
@Expose
private List<Team> team = null;
public List<Team> getTeam() {
return team;
}
public void setTeam(List<Team> team) {
this.team = team;
}
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;
}
}
Upvotes: 0