{
"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"
}]
}
这里团队数组不是常数,它是变量。从下拉列表中选择团队,为不同的团队选择不同的ID。如何使用变量数组创建模型类。团队ID不同,如910358,801345
答案 0 :(得分:1)
使用this网站创建POJO课程http://www.jsonschema2pojo.org/
更新
我创建了一个解析这个类型json的方法。试试这个
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;
}
使用示例
try {
ArrayList<Teams> teamses= teamsJsonParsing(json);
Log.e("team",teamses.size()+"");
} catch (JSONException e) {
e.printStackTrace();
}
您的模型类
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;
}
}
答案 1 :(得分:0)
您的模型类
@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;
}
}
答案 2 :(得分:0)
您的模型类必须是这样的......
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;
}
}
}