gpt4 book ai didi

android - 如何从嵌套的json数组android中获取数据

转载 作者:行者123 更新时间:2023-11-29 15:38:37 25 4
gpt4 key购买 nike

到目前为止,我正在尝试获取嵌套 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"
}
]
}
]
]

最佳答案

使用list made changes in your class copy并生成getter setter

挑战模型

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
}

挑战项目

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
}

挑战JsonConvert

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

关于android - 如何从嵌套的json数组android中获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45277540/

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