简体   繁体   中英

How to access nested array within json data

I want to access the leagueTable array within this JSON data. The shortened JSON looks like this, each array does not show all it's data to save scrolling

How would I access values within the leagueTable array?

 {
   "data":{
      "fixtures":[
         {
            "fixtureId":"4950418",
            "fixtureDate":1535806800,
            "fixtureStatus":"result",
            "fixtureComment":"",
            "competitionId":"131409",
            "competitionName":"DAWFL Reserve Division 1",
            "competitionShortName":"Res 1",
            "competitionGroupId":"",
            "seasonId":"2000",
            "compYear":"2018",
            "ageid":"3870",
            "ageName":"Div",
            "gender":"m",
            "round":null,
            "adminnote":"",
            "metaData":"",
            "competitioncomment":"02 Oct 2018 - Clonduff II deducted 3 points - Misconduct (Rule 13.13.1)",
            "competitionDispResults":"1",
            "homeTeam":"3rd Bangor II",
            "awayTeam":"Newtown Forest II",
            "homeClub":"3rd Bangor",
            "awayClub":"Newtown Forest",
            "homeClubAlternateName":"",
            "awayClubAlternateName":"",
            "homeTeamId":"223739",
            "awayTeamId":"223754",
            "homeClubId":"48049",
            "awayClubId":"48069",
            "homeClubLogo":"http:\/\/sportlomo-userupload.s3.amazonaws.com\/clubLogos\/48049\/3rdBangorOB.jpg",
            "awayClubLogo":"",
            "scoreMetadata":[

            ],
            "homeScore":"0;",
            "awayScore":"2;",
            "venue":"Valentines Playing Fields",
            "venuePostalCode":"",
            "homeDrop":null,
            "homePen":null,
            "homeConv":null,
            "awayDrop":null,
            "awayPen":null,
            "awayConv":null,
            "officials":{
               "Referee":null,
               "Referee2":null,
               "Assessor":null,
               "Results Official":null
            },
            "matchOfficials":{
               "refid":{
                  "role":null,
                  "name":"3rd Bangor OB II (Pending)",
                  "refID":"54540"
               }
            }
         }
],
"leagueTable":[
         {
            "Pen":0,
            "Drop":0,
            "Conv":0,
            "team_id":223750,
            "club_id":"48065",
            "club_logo":"",
            "team":"Comber YM II",
            "played":15,
            "gamesWon":14,
            "gamesDraw":0,
            "gameLost":1,
            "pointsFor":59,
            "scoredraw":0,
            "pointsAgainst":11,
            "pointsDifference":48,
            "goalsFor":59,
            "goalsAgainst":11,
            "goalsDifference":48,
            "bonusPoints":0,
            "bonusPointsW":0,
            "bonusPointsL":0,
            "bonusPointsM":0,
            "winsPercentage":"0.933",
            "teamDeduction":0,
            "setQuotient":0,
            "points":42,
            "scoresFor":0,
            "scoresAgainst":0,
            "3-0":0,
            "3-1":0,
            "3-2":0,
            "2-3":0,
            "1-3":0,
            "0-3":0,
            "gamesBehind":0,
            "fpp":5
         }
 ]
   },
   "settings":{
      "columns":{
         "team":"Team",
         "played":"Pld",
         "gamesWon":"W",
         "gameLost":"L",
         "gamesDraw":"D",
         "pointsFor":"+",
         "pointsAgainst":"-",
         "pointsDifference":"+\/-",
         "points":"Pts"
      },
      "split":"{}",
      "doleaguetable":"yes",
      "type":"league",
      "competitionName":"DAWFL Reserve Division 1",
      "displayResults":"1",
      "displayLeagueTable":"1",
      "displayTeams":"1",
      "displayStats":"1",
      "displayFormGuide":"1",
      "logo":"http:\/\/sportlomo-userupload.s3.amazonaws.com\/competitions\/131409_",
      "scoreFormat":"gaa1",
      "dateFormat":"EU"
   }
}

I have been using Gson
(dataRead is the json data as a string)

System.out.println(dataRead);
        Data data = new Gson().fromJson(dataRead, Data.class);
        System.out.println(data.leagueTable[0].team);

My Data class looks like this

public class Data {
    LeagueTable[] leagueTable;

    public Data(LeagueTable[] leagueTable) {
        this.leagueTable=leagueTable;
    }


    public static class LeagueTable{
        String team;
        int played, gamesWon, gamesDraw, gameLost, goalsFor, goalsAgainst, goalsDifference, points;
        public LeagueTable(String team, int played, int gamesWon, int gamesDraw, int gameLost, int goalsFor,
                int goalsAgainst, int goalsDifference, int points) {
            this.team = team;
            this.played = played;
            this.gamesWon = gamesWon;
            this.gamesDraw = gamesDraw;
            this.gameLost = gameLost;
            this.goalsFor = goalsFor;
            this.goalsAgainst = goalsAgainst;
            this.goalsDifference = goalsDifference;
            this.points = points;
        }


    }
}

I should be getting Comber YM II but I keep getting the NullPointerException .

You need to create Root class which contains data property. See below model:

class Root {

    private Data data;

    // getters, setters, toString
}

class Data {
    LeagueTable[] leagueTable;

    // getters, setters, toString

    public static class LeagueTable{
        String team;
        int played, gamesWon, gamesDraw, gameLost, goalsFor, goalsAgainst, goalsDifference, points;

        // getters, setters, toString
    }
}

You can deserialise it as below:

Data data = new Gson().fromJson(dataRead, Root.class).getData();

See also:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM