在未知长度的JSONArray中获取JSONArray

时间:2018-09-30 14:19:49

标签: java json

在我的应用中,我从服务器获取了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);
        }       
}

1 个答案:

答案 0 :(得分:0)

我更喜欢使用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()