![](/img/trans.png)
[英]How to convert List<JsonObject> to JsonArray in JAVA
[英]Java How to get collection(List) of specific values in JsonObject contains a JSONArray
我是 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;
}
我的 json 返回看起来像
{
"status":"BestPlayers",
"teamOne":[
{
"winningScore":"11",
"playerName":"John"
},
{
"winningScore":"11",
"playerName":"David"
}
],
"teamTwo":[
{
"winningScore":"15",
"playerName":"Victor"
},
{
"winningScore":"15",
"playerName":"Thomas"
}
]
}
现在我正在尝试获取两支球队的球员名单。 应该看起来 [John, David, Victor,Thomas]
我尝试了一个 while 循环,它可以循环 arrays 上的任何数字,但无法做到这一点,但我只得到了第一个球员的名字,仅此而已,我什至无法到达第二个团队阵列。 我将衷心感谢您的帮助。
需要有关此代码的帮助
问题在于您的 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;
}
现在创建一个玩家的最终名单,比如
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:
}
然后使用 foreach 循环
for(Team team: finalTeams){
Log.d("playerName: ", team.PlayerName)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.