1

I'm trying to deserialize a JSON object with Jackson in Java. I want to put each team of my JSON stream into a team object.

Here's the JSON:

{
    "resource": "commonteamyears",
    "parameters": {
        "LeagueID": "00"
    },
    "resultSets": [
        {
            "name": "TeamYears",
            "headers": [
                "LEAGUE_ID",
                "TEAM_ID",
                "MIN_YEAR",
                "MAX_YEAR",
                "ABBREVIATION"
            ],
            "rowSet": [
                [
                    "00",
                    1610612737,
                    "1949",
                    "2015",
                    "ATL"
                ],
                [
                    "00",
                    1610612738,
                    "1946",
                    "2015",
                    "BOS"
                ],
                [
                    "00",
                    1610612739,
                    "1970",
                    "2015",
                    "CLE"
                ],
                [
                    "00",
                    1610612740,
                    "2002",
                    "2015",
                    "NOP"
                ],
                [
                    "00",
                    1610612741,
                    "1966",
                    "2015",
                    "CHI"
                ],
                [
                    "00",
                    1610612742,
                    "1980",
                    "2015",
                    "DAL"
                ],
                [
                    "00",
                    1610612743,
                    "1976",
                    "2015",
                    "DEN"
                ],
                [
                    "00",
                    1610612744,
                    "1946",
                    "2015",
                    "GSW"
                ],
                [
                    "00",
                    1610612745,
                    "1967",
                    "2015",
                    "HOU"
                ],
                [
                    "00",
                    1610612746,
                    "1970",
                    "2015",
                    "LAC"
                ],
                [
                    "00",
                    1610612747,
                    "1948",
                    "2015",
                    "LAL"
                ],
                [
                    "00",
                    1610612748,
                    "1988",
                    "2015",
                    "MIA"
                ],
                [
                    "00",
                    1610612749,
                    "1968",
                    "2015",
                    "MIL"
                ],
                [
                    "00",
                    1610612750,
                    "1989",
                    "2015",
                    "MIN"
                ],
                [
                    "00",
                    1610612751,
                    "1976",
                    "2015",
                    "BKN"
                ],
                [
                    "00",
                    1610612752,
                    "1946",
                    "2015",
                    "NYK"
                ],
                [
                    "00",
                    1610612753,
                    "1989",
                    "2015",
                    "ORL"
                ],
                [
                    "00",
                    1610612754,
                    "1976",
                    "2015",
                    "IND"
                ],
                [
                    "00",
                    1610612755,
                    "1949",
                    "2015",
                    "PHI"
                ],
                [
                    "00",
                    1610612756,
                    "1968",
                    "2015",
                    "PHX"
                ],
                [
                    "00",
                    1610612757,
                    "1970",
                    "2015",
                    "POR"
                ],
                [
                    "00",
                    1610612758,
                    "1948",
                    "2015",
                    "SAC"
                ],
                [
                    "00",
                    1610612759,
                    "1976",
                    "2015",
                    "SAS"
                ],
                [
                    "00",
                    1610612760,
                    "1967",
                    "2015",
                    "OKC"
                ],
                [
                    "00",
                    1610612761,
                    "1995",
                    "2015",
                    "TOR"
                ],
                [
                    "00",
                    1610612763,
                    "1995",
                    "2015",
                    "MEM"
                ],
                [
                    "00",
                    1610612764,
                    "1961",
                    "2015",
                    "WAS"
                ],
                [
                    "00",
                    1610612765,
                    "1948",
                    "2015",
                    "DET"
                ],
                [
                    "00",
                    1610612766,
                    "1988",
                    "2015",
                    "CHA"
                ],
                [
                    "00",
                    1610612762,
                    "1974",
                    "2015",
                    "UTA"
                ],
                [
                    "00",
                    1610610029,
                    "1948",
                    "1948",
                    null
                ],
                [
                    "00",
                    1610610025,
                    "1946",
                    "1949",
                    null
                ],
                [
                    "00",
                    1610610034,
                    "1946",
                    "1949",
                    null
                ],
                [
                    "00",
                    1610610036,
                    "1946",
                    "1950",
                    null
                ],
                [
                    "00",
                    1610610024,
                    "1947",
                    "1954",
                    null
                ],
                [
                    "00",
                    1610610027,
                    "1949",
                    "1949",
                    null
                ],
                [
                    "00",
                    1610610030,
                    "1949",
                    "1952",
                    null
                ],
                [
                    "00",
                    1610610033,
                    "1949",
                    "1949",
                    null
                ],
                [
                    "00",
                    1610610037,
                    "1949",
                    "1949",
                    null
                ],
                [
                    "00",
                    1610610031,
                    "1946",
                    "1946",
                    null
                ],
                [
                    "00",
                    1610610023,
                    "1949",
                    "1949",
                    null
                ],
                [
                    "00",
                    1610610028,
                    "1946",
                    "1946",
                    null
                ],
                [
                    "00",
                    1610610026,
                    "1946",
                    "1946",
                    null
                ],
                [
                    "00",
                    1610610032,
                    "1946",
                    "1948",
                    null
                ],
                [
                    "00",
                    1610610035,
                    "1946",
                    "1946",
                    null
                ]
            ]
        }
    ]
}

I've created a POJO JsonRequestResult and I can get three parameters : resource, parameters and resultSets. But from here, I don't know how to get access to the data in resultSets.

Here's my main method:

public static void main(String[] args) throws JsonParseException, JsonMappingException, MalformedURLException, IOException {
    ObjectMapper objectMapper = new ObjectMapper();

    JsonRequestResult jrr = objectMapper.readValue(new URL("http://stats.nba.com/stats/commonteamyears/?LeagueID=00"), JsonRequestResult.class);

    System.out.println(jrr.getResource());
    System.out.println(jrr.getParameters());
    System.out.println(jrr.getResultSets());
}

And here's my class JsonRequestResult:

public class JsonRequestResult {

    private String resource;
    private Object parameters;
    private Object[] resultSets;

}
blacktide
  • 10,654
  • 8
  • 33
  • 53
StYoukii
  • 11
  • 2
  • I don't understand what you are asking for. Are the objects that you receive empty/null? In other words: why don't you show us the output of your program; and tell us where that deviates from your expectations? – GhostCat Jun 08 '16 at 13:31
  • Possible duplicate of [Converting Java objects to JSON with Jackson](http://stackoverflow.com/questions/15786129/converting-java-objects-to-json-with-jackson) – blacktide Jun 08 '16 at 13:34
  • To be clear, I don't know what I have to write to create an object Java corresponding to my Json stream. – StYoukii Jun 08 '16 at 13:48

1 Answers1

1

You need nested classes:

public class JsonRequestResult {
    private String resource;
    private Object parameters;
    private ResultSetEntry[] resultSets;
}

public class ResultSetEntry {
    private String name;
    ...
}

you see where this goes?

Benjamin M
  • 23,599
  • 32
  • 121
  • 201