2016-11-07 47 views
0

我已经下载一个JSON文件有这样一个领域:错误此双重转换以JSON

enter image description here

,所以我做了这个代码:

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()); 
       } 
      }); 

但问题是某些字段为空/0.0:在这种情况下,日志是latitude_deg = 0.0iso_country = null。这是机场类:

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; 
} 

}

,我已经与JsonToPOJO模式生成。

检索我的0 /空值的错误是什么?

谢谢

+0

因此,您的问题是,值没有填充? –

+0

@Hans Wurst它不需要从Json的值 – nani

回答

2

的问题是,GSON不知道如何映射“latitude_deg”到latitudeDeg。您需要提供注释,如以下示例所示:

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()); 
} 
+0

我使用你的代码,但它给了我一些错误: com.google.gson.JsonSyntaxException:java.lang.NumberFormatException:无效的双:“”,为什么?我可以将它作为字符串下载吗? – nani

+0

可以请你发布json,它会抛出异常吗? –

+0

Json文件与我在问题中发布的相同,只需添加[{...}] – nani