[英]Java How to get collection(List) of specific values in JsonObject contains a JSONArray
I am new to Java and I have a javabeans conatins我是 Java 的新手,我有一个 javabeans conatins
class Players{
@SerializedName("status")
@Expose
private String status;
@SerializedName("teamOne")
@Expose
private List<TeamOne> teamOne;
@SerializedName("teamTwo")
@Expose
private List<TeamTwo> teamTwo;
public String getStatus() {return status);}
public void setStatus(String status) {this.status = status;}
public List<TeamOne> getTeamOne(){ return teamOne:}
public setTeamOne(List<TeamOne> teamOne){ this.teamOne= teamOne:}
public List<TeamTwo> getTeamTwo(){ return teamTwo:}
public setTeamTwo(List<TeamTwo> teamTwo){ this.teamTwo= teamTwo;}
}
class TeamOne {
@SerializedName("winningScore")
@Expose
private String winningScore;
@SerializedName("playerName")
@Expose
private String PlayerName;
}
class TeamTwo {
@SerializedName("winningScore")
@Expose
private String winningScore;
@SerializedName("playerName")
@Expose
private String PlayerName;
}
My json return looks like我的 json 返回看起来像
{
"status":"BestPlayers",
"teamOne":[
{
"winningScore":"11",
"playerName":"John"
},
{
"winningScore":"11",
"playerName":"David"
}
],
"teamTwo":[
{
"winningScore":"15",
"playerName":"Victor"
},
{
"winningScore":"15",
"playerName":"Thomas"
}
]
}
Now I am trying to get List of the players Name in both teams.现在我正在尝试获取两支球队的球员名单。 which should look [John, David, Victor,Thomas]应该看起来 [John, David, Victor,Thomas]
I tried a while loop which could loop what ever the number on arrays but could not do that, but am getting only the first players name and thats it, I could not even reach to the second team array.我尝试了一个 while 循环,它可以循环 arrays 上的任何数字,但无法做到这一点,但我只得到了第一个球员的名字,仅此而已,我什至无法到达第二个团队阵列。 I would really appreciate your help.我将衷心感谢您的帮助。
need help with this code需要有关此代码的帮助
the problem is with your POJO Model class its should be TeamDetails
or Team
问题在于您的 POJO Model class 它应该是TeamDetails
或Team
class Players{
@SerializedName("status")
@Expose
private String status;
@SerializedName("teamOne")
@Expose
private List<Team> teamOne;
@SerializedName("teamTwo")
@Expose
private List<Team> teamTwo;
}
class Team {
@SerializedName("winningScore")
@Expose
private String winningScore;
@SerializedName("playerName")
@Expose
private String PlayerName;
}
now create a Final List of players like现在创建一个玩家的最终名单,比如
private getTotalList(){
List<Team> finalTeams=new ArrayList()
if(null!=teamOne && teamOne.size()>0){ // thats how your requirment will work
finalTeams.add(teamOne)
}
//same goes for all teams add null check and add them.
if(null!=teamTwo && teamTwo .size()>0){ // thats how your requirment will work
finalTeams.add(teamTwo)
}
if(null!=funLovers && funLovers .size()>0){
finalTeams.add(funLovers)
}
return finalTeams:
}
then use foreach loop然后使用 foreach 循环
for(Team team: finalTeams){
Log.d("playerName: ", team.PlayerName)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.