【问题标题】:Gson Object complex with list objects (from json to complex object)带有列表对象的 Gson Object complex(从 json 到复杂对象)
【发布时间】:2016-09-05 17:32:02
【问题描述】:

执行时出现错误

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;

}

【问题讨论】:

    标签: json list gson android


    【解决方案1】:

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

    JSON对象和数组有很大区别:

    这是 JSON 对象:

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

    这是 JSON 数组:

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

    注意这些括号:[]

    解决方案:

    f 字段更改为数组:

    private TiposCombustibleResponseDTO[] f;
    

    【讨论】:

    • 我已添加但将 json 放在这里:{"a":5,"b":50,"c":23,"d":"12349","e":2, "f":[{"a":2,"b":"Premium"},{"a":1,"b":"Magna"}]}
    • VehiculoCombustibleResponseDTO 是一个复杂的对象(你可以在 json 字符串中看到)
    • 我要从json到object
    • 你有理由相信我有 f 字段为 List f 我已将其更新为 List,现在它可以工作了。
    • Gson 可以自动将数组包装到列表中。我应该注意到它:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-02
    • 2015-05-28
    • 1970-01-01
    • 1970-01-01
    • 2018-03-28
    相关资源
    最近更新 更多