Deserialization Gson X Android

Asked

Viewed 43 times

-2

I am unable to pass the value of the JSON string to an entity (OBJECT).

I’ve already managed to receive JSON, and code 200 success. But I can’t put that value on the object (Deserialization).

if (response.statusCode == ConversorConstantsAPI.STATUS_CODE.SUCCESS) {
            // Deserialization
            Gson gson = new Gson();
            DolarDia dolarDia = gson.fromJson(response.json, DolarDia.class);

            //sponse.setResult(new Gson().fromJson(response.json, DolarDia.class));

            String line = "";

            result.setResult(dolarDia);

Dolardia class

public class DolarDia {
    public String mContext;
    public DolarDiaValores dolarDiaValores;
}

Class Values Dolar

public class DolarDia {
    public String mContext;
    public DolarDiaValores dolarDiaValores;
}

Value received:

response.json {"@odata.context":"https://was-p.bcnet.bcb.gov.br/olinda/servico/PTAX/versao/v1/odata$metadata#_CotacaoDolarDia(cotacaoCompra,cotacaoVenda,dataHoraCotacao)","value":[{"cotacaoCompra":3.96440,"cotacaoVenda":3.96500,"dataHoraCotacao":"2019-05-02 13:10:38.779"}]}



response = {APIResponse@4408} 
 json = "{"@odata.context":"https://was-p.bcnet.bcb.gov.br/olinda/servico/PTAX/versao/v1/odata$metadata#_CotacaoDolarDia(cotacaoCompra,cotacaoVenda,dataHoraCotacao)","value":[{"cotacaoCompra":3.96440,"cotacaoVenda":3.96500,"dataHoraCotacao":"2019-05-02 13:10:38.779"}]}"
 statusCode = 200
 shadow$_klass_ = {Class@4312} 
 shadow$_monitor_ = -1901606490

1 answer

0

I made a change in the classes, I believe that starting from there it goes of right

public class Dolardia { public String mContext; public list values;

}

public class Dolardiavalores { public float quotoCompra = 0; public cotacaoVenda float = 0; public string dateHoraCotacao = "date";

}

call of the method if (Sponse.statusCode == Constantsapi.STATUS_CODE.SUCCESS) {

            // Deserialization
            Gson gson = new Gson();
            DolarDia dolarDia = gson.fromJson(response.json, DolarDia.class);

            String line = "";

            result.setResult(dolarDia);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.