如何访问JSON数据中的嵌套数组

时间:2019-04-03 18:06:40

标签: java json gson deserialization json-deserialization

我想访问此leagueTable数据中的JSON数组。缩短后的JSON如下所示,每个数组并没有显示所有数据以保存滚动

如何访问LeagueTable数组中的值?

 {
   "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"
   }
}

我一直在使用Gson
(dataRead是作为字符串的json数据)

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

我的Data类看起来像这样

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


    }
}

我应该得到Comber YM II,但是我一直得到NullPointerException

1 个答案:

答案 0 :(得分:0)

您需要创建包含Root属性的data类。参见以下模型:

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

您可以如下将其反序列化:

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

另请参阅: