TapateBen
TapateBen

Reputation: 23

I can't parse a Json file to a java object using Gson

I have a Json file like this:

{
 "airports": [
  {
   "fs": "VGO",
   "iata": "VGO",
   "icao": "LEVX",
   "name": "Vigo Airport",
   "city": "Vigo",
   "cityCode": "VGO",
   "stateCode": "SP",
   "countryCode": "ES",
   "countryName": "Spain and Canary Islands",
   "regionName": "Europe",
   "timeZoneRegionName": "Europe/Madrid",
   "localTime": "2018-01-29T08:59:15.661",
   "utcOffsetHours": 1,
   "latitude": 42.224551,
   "longitude": -8.634025,
   "elevationFeet": 860,
   "classification": 4,
   "active": true,
   "weatherUrl": "https://api.flightstats.com/flex/weather/rest/v1/json/all/VGO?codeType=fs",
   "delayIndexUrl": "https://api.flightstats.com/flex/delayindex/rest/v1/json/airports/VGO?codeType=fs"
  }
 ]
}

and I want to use to create an airport object.

public class Airport {

    String iata;
    String name;
    String city;
    String countryName;
    String regionName;
    String timeZoneRegionName;
    double utcOffsetHours;

    double latitude;
    double longitude;
    int elevationFeet;

    @Override
    public String toString() {
        return "Airports{" +
                "iata='" + iata + '\'' +
                ", name='" + name + '\'' +
                ", city='" + city + '\'' +
                ", countryName='" + countryName + '\'' +
                ", regionName='" + regionName + '\'' +
                ", timeZoneRegionName='" + timeZoneRegionName + '\'' +
                ", utcOffsetHours=" + utcOffsetHours +
                ", latitude=" + latitude +
                ", longitude=" + longitude +
                ", elevationFeet=" + elevationFeet +
                '}';
    }
}

and I read it in the following way:

public void imprimirJson(String fileName) {

        String filePath = getCacheDir() + "/" + fileName + ".json";
        Gson gson = new Gson();
        Airport airport = null;

        try {
            airport = gson.fromJson(new FileReader(filePath), Airport.class);
        } catch (FileNotFoundException e) {
        }
        Log.i("MSG", airport.toString());
    }

But if I execute this code, the Log prints an empty array

public void printJson(String fileName) {

        String filePath = getCacheDir() + "/" + fileName + ".json";
        Gson gson = new Gson();
        Airport airport = null;

        try {
            airport = gson.fromJson(new FileReader(filePath), Airport.class);
        } catch (FileNotFoundException e) {
        }
        Log.i("MSG", airport.toString());
    }

I think that the problem is that the first attribute, has an array of the info that I want. But I don't know how to access the info. Can you show me the way?

Upvotes: 2

Views: 842

Answers (2)

Aswin P Ashok
Aswin P Ashok

Reputation: 711

create a class MyAirports.java.

public class MyAirports{
    List<Airport> airports;
    public List<Airport> getAirportList()
    {
        return this.airports;
    }
}

and do,

 public void printJson(String fileName) {

    String filePath = getCacheDir() + "/" + fileName + ".json";
    Gson gson = new Gson();
    MyAirports airports = null;

    try {
        //airport = gson.fromJson(new FileReader(filePath), Airport.class);
        airports = gson.fromJson(new FileReader(filePath), MyAirports.class);
    } catch (FileNotFoundException e) {
    }
    Log.i("MSG", airports.getAirportList().get(0).toString());
}

Upvotes: 1

Cao Minh Vu
Cao Minh Vu

Reputation: 1950

The value of "airports" in your json file is a JsonArray. Hence, you can implement like this:

public void imprimirJson(String fileName) {

        String filePath = getCacheDir() + "/" + fileName + ".json";
        Gson gson = new Gson();
        Airport[] airport = null;

        try {
            airport = gson.fromJson(new FileReader(filePath), Airport[].class);
        } catch (FileNotFoundException e) {
        }
        Log.i("MSG", airport.toString());
    }

Later, airport[0] is what you want to print out.

Upvotes: 0

Related Questions