基本上我试图遍历JSONArray并基于其中一个对象中的一个键的值,我试图获取同一对象中另一个键的值。
更具体地说,我有一个值,我想传递给一个方法,并在该方法中我想循环通过一个JSONArray,当循环找到具有我传入的值的JSONObject时,我想要使用该对象设置新值。
我尝试循环遍历JSONArray并在模型类中设置值,然后在循环找到我要查找的值时从同一模型类获取。
我是初学程序员,所以我不确定自己是否走在正确的轨道上。
以下是我一直尝试的方法:
private String getTeamAbbrev(String jsonData, Integer opponentTeamId) {
Team[] teams = null;
String opponentAbbrev = null;
try {
JSONArray root = new JSONArray(jsonData);
teams = new Team[root.length()];
for (int i = 0; i < root.length(); i++){
JSONObject jsonTeam = root.getJSONObject(i);
Team team = new Team();
team.setTeamId(jsonTeam.getInt("team_id"));
team.setCity(jsonTeam.getString("city"));
team.setAbbrev(jsonTeam.getString("abbreviation"));
team.setFullName(jsonTeam.getString("team_name"));
teams[i] = team;
if(team.getTeamId() == opponentTeamId)) {
opponentAbbrev = team.getAbbrev();
}
}
} catch (JSONException e) {
e.printStackTrace();
}
return opponentAbbrev;
}
这是我制作的模型类:
public class Team {
private String mAbbrev;
private String mCity;
private Integer mTeamId;
private String mFullName;
public String getAbbrev() {
return mAbbrev;
}
public void setAbbrev(String abbrev) {
mAbbrev = abbrev;
}
public String getCity() {
return mCity;
}
public void setCity(String city) {
mCity = city;
}
public Integer getTeamId() {
return mTeamId;
}
public void setTeamId(Integer teamId) {
mTeamId = teamId;
}
public String getFullName() {
return mFullName;
}
public void setFullName(String fullName) {
mFullName = fullName;
}
}
JSON看起来像这样:
[ { "abbreviation" : "ATL",
"city" : "Atlanta",
"team_id" : 1610612737,
"team_name" : "Hawks"
},
{ "abbreviation" : "BOS",
"city" : "Boston",
"team_id" : 1610612738,
"team_name" : "Celtics"
},
{ "abbreviation" : "CLE",
"city" : "Cleveland",
"team_id" : 1610612739,
"team_name" : "Cavaliers"
},
{ "abbreviation" : "NOP",
"city" : "New Orleans",
"team_id" : 1610612740,
"team_name" : "Pelicans"
}]
基于&#34; team_id&#34;我从一个单独的api请求得到了,我想得到&#34;缩写&#34;的价值。键。
这是调试行
if(team.getTeamId() == (opponentTeamId)) {opponentAbbrev = team.getAbbrev(); }
this = {oneonanyone.com.fantasybasketball_1onany1.MatchupActivity@831701908048}
jsonData = {java.lang.String@831708371144}"[ { "abbreviation" : "ATL",\n "city" : "Atlanta",\n "team_id" : 1610612737,\n "team_name" : "Hawks"\n },\n { "abbreviation" : "BOS",\n "city" : "Boston",\n "team_id" : 1610612738,\n "team_name" : "Celtics"\...
opponentTeamId = {java.lang.Integer@831708367536}"1610612740"
value = 1610612740
teams = {oneonanyone.com.fantasybasketball_1onany1.DataModel.Team[30]@831708408096}
[0] = {oneonanyone.com.fantasybasketball_1onany1.DataModel.Team@831708408232}
mAbbrev = {java.lang.String@831708378296}"ATL"
mCity = {java.lang.String@831708378536}"Atlanta"
mFullName = {java.lang.String@831708378944}"Hawks"
value = {char[5]@831708379008}
hashCode = 0
offset = 0
count = 5
mTeamId = {java.lang.Integer@831708408328}"1610612737"
opponentAbbrev = null
root = {org.json.JSONArray@831708378040}"[{"abbreviation":"ATL","team_id":1610612737,"team_name":"Hawks","city":"Atlanta"},{"abbreviation":"BOS","team_id":1610612738,"team_name":"Celtics","city":"Boston"},{"abbreviation":"CLE","team_id":1610612739,"team_name"...
values = {java.util.ArrayList@831708378096} size = 30
i = 0
jsonTeam = {org.json.JSONObject@831708378120}"{"abbreviation":"ATL","team_id":1610612737,"team_name":"Hawks","city":"Atlanta"}"
nameValuePairs = {java.util.HashMap@831708378136} size = 4
team = {oneonanyone.com.fantasybasketball_1onany1.DataModel.Team@831708408232}
mAbbrev = {java.lang.String@831708378296}"ATL"
mCity = {java.lang.String@831708378536}"Atlanta"
mFullName = {java.lang.String@831708378944}"Hawks"
mTeamId = {java.lang.Integer@831708408328}"1610612737"
teams[i] = {oneonanyone.com.fantasybasketball_1onany1.DataModel.Team@831708408232}
mAbbrev = {java.lang.String@831708378296}"ATL"
mCity = {java.lang.String@831708378536}"Atlanta"
mFullName = {java.lang.String@831708378944}"Hawks"
mTeamId = {java.lang.Integer@831708408328}"1610612737"
感谢您的帮助!
答案 0 :(得分:0)
您不应该使用 if(team.getTeamId().equals(opponentTeamId)) {
opponentAbbrev = team.getAbbrev().toString();
}
进行字符串比较。
试试这个:
{{1}}祝你好运。