0

I am trying to parse JSON using Retrofit and I am wondering if there is a way to skip or pass over the root object. Below is a snippet of the JSON.

{
"?xml": {
    "@version": "1.0",
    "@encoding": "utf-8"
},
"root": {
    "uri": {
        "#cdata-section": "http://api.bart.gov/api/stn.aspx?cmd=stns&json=y"
    },
    "stations": {
        "station": [
            {
                "name": "12th St. Oakland City Center",
                "abbr": "12TH",
                "gtfs_latitude": "37.803768",
                "gtfs_longitude": "-122.271450",
                "address": "1245 Broadway",
                "city": "Oakland",
                "county": "alameda",
                "state": "CA",
                "zipcode": "94612"
            },
            {
                "name": "West Oakland",
                "abbr": "WOAK",
                "gtfs_latitude": "37.804872",
                "gtfs_longitude": "-122.295140",
                "address": "1451 7th Street",
                "city": "Oakland",
                "county": "alameda",
                "state": "CA",
                "zipcode": "94607"
            }
        ]
    },
    "message": ""
  }
}

I have POJOs for root, stations, and station. I also have a StationList class that is listed below.

public class StationList {

  private int id;

  @SerializedName("root")
  @Expose
  private Root root;

  public int getId() {return id;}

  public Root getRoot() {
    return root;
  }
}

If I want to access the first station name ("12th St. Oakland City Center") I have to use this syntax

stationList.getRoot().getStations().getStation().get(0).getName()

Is there anyway It could be

stationList.getStation().get(0).getName();

I want to just ignore the root JSON object as I only want the list of stations.

jmcs
  • 53
  • 1
  • 6
  • Possible duplicate of [Get nested JSON object with GSON using retrofit](https://stackoverflow.com/questions/23070298/get-nested-json-object-with-gson-using-retrofit) – Giddy Naya Aug 06 '19 at 23:59
  • 1
    Why not implementing those methods in the pojos? – Fred Aug 07 '19 at 08:44

1 Answers1

0

add this method to your StationList class

public String getStationName(int i) {return getRoot().getStations().getStation().get(i).getName()}
taha
  • 731
  • 2
  • 7
  • 18