gpt4 book ai didi

android - 如何使用 JsonArray 的 gson 创建模型类,其中数组是变量而不是常量 android

转载 作者:行者123 更新时间:2023-11-30 00:23:55 25 4
gpt4 key购买 nike

{
"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。如何使用变量数组创建模型类。 team id 是不同的像这里 910358,801345

最佳答案

使用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;
}

}

关于android - 如何使用 JsonArray 的 gson 创建模型类,其中数组是变量而不是常量 android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45757106/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com