首页 > 解决方案 > 在 JSONArray 中获取长度未知的 JSONArray

问题描述

在我的应用程序中,我JSONArray从服务器获取一个。接收到的数据有这样的结构:

[
  {
    "MatchID": 51170,
    "Team1": {
      "TeamId": 1,
    },
    "Team2": {
      "TeamId": 1,
    },
    "Goals": [
      {
        "GoalID": 1,
      },
      {
        "GoalID": 2,
      },
      {
        "GoalID": 3,
      }
    ]
  }
]

我遇到的问题是其中的JSONArray“目标”长度未知。我通常通过 getter 和 setter 存储和处理所有数据,如下所示:

public String getGoalid() {

return goalid;
}

public void setGoalid(String goalid) {

this.goalid = goalid;
}

但是像这样我只是得到了最后一个目标的信息。我还尝试将 getter 和 setter 用作字符串数组,但像这样我也只存储了最后一个值。否则正确存储这些未知数量的字符串的正确方法是什么?

编辑:

这是我如何处理接收到的数据的代码:

List<GetData> GetData1 = new ArrayList<>();
 
public void Process(JSONArray array){
    for(int i = 0; i<array.length(); i++) {        
            GetData GetData2 = new GetData();
            JSONObject json = null;
            try {
                json = array.getJSONObject(i);
              
            JSONObject team1 = json.getJSONObject("Team1");
                JSONObject user = json.getJSONObject("Team2");
                GetData2.setTeam1(team1.getString(TEAM1));
                GetData2.setTeam2(user.getString(TEAM2));
               

            JSONArray jArrayGoals = json.getJSONArray("Goals");
                if(jArrayGoals == null || jArrayGoals.length() == 0){
                    
                }else {
                    for (int j = 0; j < jArrayGoals.length(); j++) {
                        JSONObject json_data = jArrayGoals.getJSONObject(j);
                        GetData2.setGoalid(json_data.getString("GoalID"));                                                 
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            GetData1.add(GetData);
        }       
}

标签: javajson

解决方案


我更喜欢使用Gsonhttps://github.com/google/gson

摇篮安装

dependencies {
  implementation 'com.google.code.gson:gson:2.8.5'
}

对于这种情况下的类

public class Team
{
  private int TeamId;

  public int getTeamId() { return this.TeamId; }

  public void setTeamId(int TeamId) { this.TeamId = TeamId; }
}


public class Goal
{
  private int GoalID;

  public int getGoalID() { return this.GoalID; }

  public void setGoalID(int GoalID) { this.GoalID = GoalID; }
}

public class RootObject
{
  private int MatchID;

  public int getMatchID() { return this.MatchID; }

  public void setMatchID(int MatchID) { this.MatchID = MatchID; }

  private Team Team1;

  public Team getTeam1() { return this.Team1; }

  public void setTeam1(Team1 Team1) { this.Team1 = Team1; }

  private Team Team2;

  public Team getTeam2() { return this.Team2; }

  public void setTeam2(Team2 Team2) { this.Team2 = Team2; }

  private ArrayList<Goal> Goals;

  public ArrayList<Goal> getGoals() { return this.Goals; }

  public void setGoals(ArrayList<Goal> Goals) { this.Goals = Goals; }
}

获得目标长度会像这样

RootObject rootObject = gson.fromJson(json, RootObject.class);
int goalLength = rootObject.getGoals().size()

推荐阅读