nani
nani

Reputation: 382

error to convert this double in Json

I've to download a Json file with a fields like this:

enter image description here

and so I've made this code:

    final Retrofit retrofit = new Retrofit.Builder()
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
            .baseUrl(AirportService.SERVICE_ENDPOINT).build();


    AirportService service = retrofit.create(AirportService.class);

    service.getAirport()
            .subscribeOn(Schedulers.newThread())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Subscriber<List<Airport>>()
            {
                List<Airport>airps = new ArrayList<Airport>();
                @Override
                public void onCompleted()
                {

                }

                @Override
                public void onError(Throwable e)
                {
                    e.printStackTrace();
                }

                @Override
                public void onNext(List<Airport> airports)
                {
                for(Airport air : airports)              
                {                 
                 Log.d("latitude",String.valueOf(air.getLatitudeDeg());
                 //Log.d("iso_c",air.getIsoCountry());
                }
            });

but the problem is that some fiels are null /0.0: in this case the log is latitude_deg = 0.0 and iso_country = null. This is the Airport class:

public class Airport {

private double latitudeDeg;
private double longitudeDeg;
private int elevationFt;
private String continent;
private String isoCountry;

/**
 *
 * @return
 * The latitudeDeg
 */
public double getLatitudeDeg() {
    return latitudeDeg;
}

/**
 *
 * @param latitudeDeg
 * The latitude_deg
 */
public void setLatitudeDeg(double latitudeDeg) {
    this.latitudeDeg = latitudeDeg;
}

/**
 *
 * @return
 * The longitudeDeg
 */
public double getLongitudeDeg() {
    return longitudeDeg;
}

/**
 *
 * @param longitudeDeg
 * The longitude_deg
 */
public void setLongitudeDeg(double longitudeDeg) {
    this.longitudeDeg = longitudeDeg;
}

/**
 *
 * @return
 * The elevationFt
 */
public int getElevationFt() {
    return elevationFt;
}

/**
 *
 * @param elevationFt
 * The elevation_ft
 */
public void setElevationFt(int elevationFt) {
    this.elevationFt = elevationFt;
}

/**
 *
 * @return
 * The continent
 */
public String getContinent() {
    return continent;
}

/**
 *
 * @param continent
 * The continent
 */
public void setContinent(String continent) {
    this.continent = continent;
}

/**
 *
 * @return
 * The isoCountry
 */
public String getIsoCountry() {
    return isoCountry;
}

/**
 *
 * @param isoCountry
 * The iso_country
 */
public void setIsoCountry(String isoCountry) {
    this.isoCountry = isoCountry;
}

}

that I've generated with JsonToPOJO schema.

What is the error that retrieve me the 0 / null value?

Thank you

Upvotes: 0

Views: 727

Answers (1)

Sergej Isbrecht
Sergej Isbrecht

Reputation: 4012

the problem is, that Gson does not know how to map "latitude_deg" to latitudeDeg. You need to provide annotations as in the following example:

private class Airport {

    @SerializedName("latitude_deg")
    private double latitudeDeg;

    @SerializedName("longitude_deg")
    private double longitudeDeg;

    @SerializedName("elevation_ft")
    private int elevationFt;

    @SerializedName("continent")
    private String continent;

    @SerializedName("iso_country")
    private String isoCountry;

    // get-/ set-methods
}

@Test
public void name() throws Exception {
    Gson gson = new Gson();

    String jsonInString = "{ \"latitude_deg\": 40.07, \"longitude_deg\": -74.07, \"elevation_ft\": 11, \"continent\": NA, \"iso_country\": \"US\" }";
    Airport staff = gson.fromJson(jsonInString, Airport.class);

    Assert.assertTrue("US".equals(staff.getIsoCountry()));
    Assert.assertTrue("NA".equals(staff.getContinent()));
    Assert.assertTrue(staff.getElevationFt() == 11);
    Assert.assertTrue(40.07d == staff.getLatitudeDeg());
    Assert.assertTrue(-74.07d == staff.getLongitudeDeg());
}

Upvotes: 2

Related Questions