2

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?

4
  • 3
    Please edit your question here "y lo leo de la siguiente forma" it must be completely in English. Commented Jan 29, 2018 at 10:20
  • Sorry, i forgot to translate it Commented Jan 29, 2018 at 10:31
  • Can you replace "y lo leo de la siguiente forma" by "and I read it in the following way" ? Thanks. Commented Jan 29, 2018 at 10:34
  • Sorry, I couldn't find the edit button in my mobile phone Commented Jan 29, 2018 at 14:34

2 Answers 2

1

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());
}
Sign up to request clarification or add additional context in comments.

1 Comment

Your solution works, but I had to put airports.airports.get(0), because Android Studio couldn't see the airports variable in the MyAirports class
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.