到目前为止,我试图获取嵌套json数组的数据但没有成功。也许有人有一个想法或方向如何正确地做到这一点。我的代码是 挑战课
public class ChallengeModel {
private String ChallengeID;
private String ChallengeDate;
private String ChallengeName;
private String ChallengeIcon;
private String BetID;
private ChallengeItem challengeItem[];
public String getChallengeID() { return ChallengeID; }
public void setChallengeID(String challengeID) {
ChallengeID = challengeID;
}
public String getChallengeDate() {
return ChallengeDate;
}
public void setChallengeDate(String challengeDate) {
ChallengeDate = challengeDate;
}
public String getChallengeName() {
return ChallengeName;
}
public void setChallengeName(String challengeName) {
ChallengeName = challengeName;
}
public String getChallengeIcon() {
return ChallengeIcon;
}
public void setChallengeIcon(String challengeIcon) {
ChallengeIcon = challengeIcon;
}
public String getBetID() {
return BetID;
}
public void setBetID(String betID) {
BetID = betID;
}
public ChallengeItem[] getChallengeItem() {
return challengeItem;
}
public void setChallengeItem(ChallengeItem[] challengeItem) {
this.challengeItem = challengeItem; }}
在上面的类中有一个嵌套类的数组
public class ChallengeItem {
private String ChallengeItemID;
private String ChallengeID;
private String MatchID;
private String TeamAName;
private String TeamBName;
private String TeamAScore;
private String TeamBScore;
private String MatchDate;
private String MatchTime;
private String Venue;
private String VenuCity;
private String TeamAValue;
private String TeamBValue;
public String getChallengeItemID() {
return ChallengeItemID;
}
public void setChallengeItemID(String challengeItemID) {
ChallengeItemID = challengeItemID;
}
public String getChallengeID() {
return ChallengeID;
}
public void setChallengeID(String challengeID) {
ChallengeID = challengeID;
}
public String getMatchID() {
return MatchID;
}
public void setMatchID(String matchID) {
MatchID = matchID;
}
public String getTeamAName() {
return TeamAName;
}
public void setTeamAName(String teamAName) {
TeamAName = teamAName;
}
public String getTeamBName() {
return TeamBName;
}
public void setTeamBName(String teamBName) {
TeamBName = teamBName;
}
public String getTeamAScore() {
return TeamAScore;
}
public void setTeamAScore(String teamAScore) {
TeamAScore = teamAScore;
}
public String getTeamBScore() {
return TeamBScore;
}
public void setTeamBScore(String teamBScore) {
TeamBScore = teamBScore;
}
public String getMatchDate() {
return MatchDate;
}
public void setMatchDate(String matchDate) {
MatchDate = matchDate;
}
public String getMatchTime() {
return MatchTime;
}
public void setMatchTime(String matchTime) {
MatchTime = matchTime;
}
public String getVenue() {
return Venue;
}
public void setVenue(String venue) {
Venue = venue;
}
public String getVenuCity() {
return VenuCity;
}
public void setVenuCity(String venuCity) {
VenuCity = venuCity;
}
public String getTeamAValue() {
return TeamAValue;
}
public void setTeamAValue(String teamAValue) {
TeamAValue = teamAValue;
}
public String getTeamBValue() {
return TeamBValue;
}
public void setTeamBValue(String teamBValue) {
TeamBValue = teamBValue;
}
public String getDrawValue() {
return DrawValue;
}
public void setDrawValue(String drawValue) {
DrawValue = drawValue;
}
private String DrawValue;
}
我的问题是,如何从json获取数据到嵌套数组(第一个数组工作正常)
public class ChallengeJsonConvert {
public static List<ChallengeModel> challengesRequest(String content){
try {
JSONArray challengesArray = new JSONArray(content);
List<ChallengeModel> challengeModelList = new ArrayList<>();
for(int i = 0; i < challengesArray.length(); i++){
JSONObject obj = challengesArray.getJSONObject(i);
ChallengeModel challenge = new ChallengeModel();
challenge.setBetID(obj.getString("BetID"));
challenge.setChallengeDate(obj.getString("ChallengeDate"));
challenge.setChallengeIcon(obj.getString("ChallengeIcon"));
challenge.setChallengeID(obj.getString("ChallengeID"));
challenge.setChallengeName(obj.getString("ChallengeName"));
challengeModelList.add(challenge);
}
return challengeModelList;
} catch (JSONException e) {
e.printStackTrace();
return null;
}
}
}
这是服务器的结果
[
{
"ChallengeID": "1",
"ChallengeDate": "18/07/2017",
"ChallengeName": "New Challenge Arrived",
"ChallengeIcon": "icon.jpg",
"BetID": "3",
"challengeItem": [
{
"CallengeItemID": "5",
"ChallengeID": "1",
"MatchID": "845",
"TeamAName": "FC Copenhagen (Den)",
"TeamBName": "Zilina (Svk)",
"TeamAScore": "0",
"TeamBScore": "0",
"MatchDate": "18.07.2017",
"Venue": "Telia Parken (København)",
"VenueCity": "København",
"MatchTime": "17:00",
"TeamAValue": "5.5",
"DrawValue": "3.05",
"TeamBValue": "1.35"
},
{
"CallengeItemID": "6",
"ChallengeID": "1",
"MatchID": "844",
"TeamAName": "Samtredia (Geo)",
"TeamBName": "Qarabag (Aze)",
"TeamAScore": "0",
"TeamBScore": "0",
"MatchDate": "18.07.2017",
"Venue": "Boris Paichadze Dinamo Arena (Tbilisi)",
"VenueCity": "Tbilisi",
"MatchTime": "17:00",
"TeamAValue": "5.5",
"DrawValue": "3.05",
"TeamBValue": "1.35"
},
{
"CallengeItemID": "7",
"ChallengeID": "1",
"MatchID": "843",
"TeamAName": "Alashkert (Arm)",
"TeamBName": "BATE (Blr)",
"TeamAScore": "0",
"TeamBScore": "0",
"MatchDate": "18.07.2017",
"Venue": "",
"VenueCity": "",
"MatchTime": "16:00",
"TeamAValue": "5.5",
"DrawValue": "3.05",
"TeamBValue": "1.35"
},
{
"CallengeItemID": "8",
"ChallengeID": "1",
"MatchID": "842",
"TeamAName": "Vardar (Mac)",
"TeamBName": "Malmo FF (Swe)",
"TeamAScore": "0",
"TeamBScore": "0",
"MatchDate": "18.07.2017",
"Venue": "",
"VenueCity": "",
"MatchTime": "16:00",
"TeamAValue": "5.5",
"DrawValue": "3.05",
"TeamBValue": "1.35"
},
{
"CallengeItemID": "9",
"ChallengeID": "1",
"MatchID": "841",
"TeamAName": "FC Astana (Kaz)",
"TeamBName": "Spartaks (Lat)",
"TeamAScore": "0",
"TeamBScore": "0",
"MatchDate": "18.07.2017",
"Venue": "",
"VenueCity": "",
"MatchTime": "14:00",
"TeamAValue": "5.5",
"DrawValue": "3.05",
"TeamBValue": "1.35"
}
]
},
{
"ChallengeID": "2",
"ChallengeDate": "18/07/2017",
"ChallengeName": "Tuesday Funday",
"ChallengeIcon": "icon.jpg",
"BetID": "3",
"challengeItem": [
{
"CallengeItemID": "1",
"ChallengeID": "2",
"MatchID": "851",
"TeamAName": "Lokomotiv Moscow",
"TeamBName": "Arsenal Tula",
"TeamAScore": "0",
"TeamBScore": "0",
"MatchDate": "18.07.2017",
"Venue": "Stadion Lokomotiv (Moskva)",
"VenueCity": "Moskva",
"MatchTime": "16:30",
"TeamAValue": "5.5",
"DrawValue": "3.05",
"TeamBValue": "1.35"
},
{
"CallengeItemID": "2",
"ChallengeID": "2",
"MatchID": "853",
"TeamAName": "Dinamo Moskva",
"TeamBName": "Spartak Moskva",
"TeamAScore": "0",
"TeamBScore": "0",
"MatchDate": "18.07.2017",
"Venue": "Arena Khimki (Khimki)",
"VenueCity": "Khimki",
"MatchTime": "16:30",
"TeamAValue": "5.5",
"DrawValue": "3.05",
"TeamBValue": "1.35"
},
{
"CallengeItemID": "3",
"ChallengeID": "2",
"MatchID": "852",
"TeamAName": "Spartak Moscow",
"TeamBName": "Dynamo Moscow",
"TeamAScore": "0",
"TeamBScore": "0",
"MatchDate": "18.07.2017",
"Venue": "Otkrytiye Arena (Moskva)",
"VenueCity": "Moskva",
"MatchTime": "16:30",
"TeamAValue": "5.5",
"DrawValue": "3.05",
"TeamBValue": "1.35"
},
{
"CallengeItemID": "4",
"ChallengeID": "2",
"MatchID": "849",
"TeamAName": "Vorskla Poltava",
"TeamBName": "Shakhtar Donetsk",
"TeamAScore": "0",
"TeamBScore": "0",
"MatchDate": "18.07.2017",
"Venue": "",
"VenueCity": "",
"MatchTime": "16:00",
"TeamAValue": "5.5",
"DrawValue": "3.05",
"TeamBValue": "1.35"
}
]
}
]
]
答案 0 :(得分:2)
在班级副本中使用列表更改并生成getter setter
ChallengeModel
public class ChallengeModel {
private String ChallengeID;
private String ChallengeDate;
private String ChallengeName;
private String ChallengeIcon;
private String BetID;
private List<ChallengeItem> challengeItem;
//TODO
//Generate Getter and setter
}
ChallengeItem
public class ChallengeItem {
private String ChallengeItemID;
private String ChallengeID;
private String MatchID;
private String TeamAName;
private String TeamBName;
private String TeamAScore;
private String TeamBScore;
private String MatchDate;
private String MatchTime;
private String Venue;
private String VenuCity;
private String TeamAValue;
private String TeamBValue;
//TODO
//Generate Getter and setter
}
<强> ChallengeJsonConvert 强>
public class ChallengeJsonConvert {
public static List<ChallengeModel> challengesRequest(String content) {
try {
JSONArray challengesArray = new JSONArray(content);
List<ChallengeModel> challengeModelList = new ArrayList<>();
for (int i = 0; i < challengesArray.length(); i++) {
JSONObject obj = challengesArray.getJSONObject(i);
ChallengeModel challenge = new ChallengeModel();
challenge.setBetID(obj.getString("BetID"));
challenge.setChallengeDate(obj.getString("ChallengeDate"));
challenge.setChallengeIcon(obj.getString("ChallengeIcon"));
challenge.setChallengeID(obj.getString("ChallengeID"));
challenge.setChallengeName(obj.getString("ChallengeName"));
JSONArray challengeItemArray = obj.getJSONArray("challengeItem");
for (int j = 0; j < challengeItemArray.length(); j++) {
JSONObject challengeObject = challengeItemArray.getJSONObject(j);
ChallengeItem challengeItem = new ChallengeItem();
challengeItem.setChallengeItemID(challengeObject.getString("CallengeItemID"));
//TODO
//Get other object and set into challengeItem
challenge.getChallengeItem().add(challengeItem);
}
challengeModelList.add(challenge);
}
return challengeModelList;
} catch (JSONException e) {
e.printStackTrace();
return null;
}
}
}