-3

I would like to unmarshal a list of players from this nested json using retrofit 2 and Gson in android:

{
    "api": {
        "status": 200,
        "message": "GET players/playerId/44",
        "results": 1,
        "filters": [
            ...
        ],
        "players": [
            {
                "firstName": "Kent",
                "lastName": "Bazemore",
                "teamId": "29",
                "yearsPro": "7",
                "collegeName": "Old Dominion",
                "leagues": {
                    "standard": {
                        "jersey": "24",
                        "active": "1",
                        "pos": "G-F"
                    }
                }
            }
        ]
    }
}

Can anyone help?

aiqency
  • 1,015
  • 1
  • 8
  • 22
ismail M
  • 195
  • 1
  • 7
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Aug 27 '21 at 03:37

2 Answers2

0

If you are using Retrofit, I would advise you to look into Gson/Jackson/Moshi converters. These help in converting your JSON responses to plain Java objects.

You should then be able to access it as

api.getPlayers() -> array of players
Arun Shankar
  • 2,295
  • 16
  • 20
0

You first need to create a model class then you can access the particular key. For example:

public class ResponseItem {


private int status;
private String message;
private List<String> players;

public int getStatus() {
    return status;
}

public void setStatus(int status) {
    this.status = status;
}

public String getMessage() {
    return message;
}

public void setMessage(String message) {
    this.message = message;
}

public List<String> getPlayers() {
    return players;
}

public void setPlayers(List<String> players) {
    this.players = players;
}

Add the following code snippet in your retrofit method:

if (response.isSuccessful()) {
  ResponseItem responseItem;
  responseItem = response.body(); 
}

P.S. Make sure to complete the model class using the same pattern.

Pratham
  • 497
  • 3
  • 7