带有列表对象的Gson Object复杂(从json到复杂对象)

时间:2016-09-05 17:32:02

标签: json list gson android

执行

时出现错误
VehiculoCombustiblesResponseDTO respuesta= new Gson().fromJson(response.toString(), VehiculoCombustiblesResponseDTO.class);

错误是:

  

未处理的异常:com.google.gson.JsonSyntaxException:   java.lang.IllegalStateException:预期BEGIN_OBJECT但是   BEGIN_ARRAY在第1行第52列路径$ .f

这是json的回复

{"a":5,"b":50,"c":23,"d":"12349","e":2,"f":[{"a":2,"b":"Premium"},{"a":1,"b":"Magna"}]}

我有这个班级

public class VehiculoCombustiblesResponseDTO {
    private int a;
    private  int b;

    /**
     * VehiculoId
     * @return
     */
    public int getA() {
        return a;
    }
    /**
     * VehiculoId
     * @return
     */
    public void setA(int a) {
        this.a = a;
    }

    /**
     * CapacidadCombustible
     * @return
     */
    public int getB() {
        return b;
    }

    /**
     * CapacidadCombustible
     * @return
     */
    public void setB(int b) {
        this.b = b;
    }

    /**
     * KilometrajeInicial
     * @return
     */
    public int getC() {
        return c;
    }
    /**
     * KilometrajeInicial
     * @return
     */
    public void setC(int c) {
        this.c = c;
    }

    /**
     * NumeroEconomico
     * @return
     */
    public String getD() {
        return d;
    }
    /**
     * NumeroEconomico
     * @return
     */
    public void setD(String d) {
        this.d = d;
    }

    /**
     * TipoCombustibleId
     * @return
     */
    public int getE() {
        return e;
    }
    /**
     * TipoCombustibleId
     * @return
     */
    public void setE(int e) {
        this.e = e;
    }
    /**
     * TiposCombustible (Listado)
     * @return
     */
    public TiposCombustibleResponseDTO getF() {
        return f;
    }
    /**
     * TiposCombustible (Listado)
     * @return
     */
    public void setF(TiposCombustibleResponseDTO f) {
        this.f = f;
    }

    private  int c;
    private  String d;
    private int e;
    private TiposCombustibleResponseDTO f;
}

和其他

public class TiposCombustibleResponseDTO {

    /**
     * obtiene CombustibleId del servidor
     * @return
     */
    public int getA() {
        return a;
    }

    /**
     * Establece CombustibleId
     * @param a
     */
    public void setA(int a) {
        this.a = a;
    }

    /**
     * obtiene Descripcion del servidor
     * @return
     */
    public String getB() {
        return b;
    }

    /**
     * Estaclece Descripcion del servidor
     * @param b
     */
    public void setB(String b) {
        this.b = b;
    }

    private int a;
    private String b;

}

1 个答案:

答案 0 :(得分:0)

你对Gson说:从这个JSON给我对象,Gson找到了一个对象,但JSON响应中的f是一个数组,但在你的类中这个字段是一个对象,所以这是不兼容的。 / p>

JSON对象和数组之间存在显着差异:

这是JSON对象:

{ "firstName":"John", "lastName":"Doe" }

这是JSON数组:

[
    { "firstName":"John", "lastName":"Doe" }, 
    { "firstName":"Anna", "lastName":"Smith" }, 
    { "firstName":"Peter","lastName":"Jones" }
]

请注意这些括号:[]

<强>解决方案:

f字段更改为数组:

private TiposCombustibleResponseDTO[] f;