Convert JSON String to an Object - Java Android

Asked

Viewed 72 times

1

Hello, I’m getting the JSON string below, but I can’t put the values in the Class I created, all values are null.

Any hint?

JSON

{"USDT":{"code":"USD",
         "codein":"BRLT",
         "name":"Dólar Turismo",
         "high":"4.09",
         "low":"3.9",
         "varBid":"0.13",
         "pctChange":"3.103",
         "bid":"4.08",
         "ask":"4.32",
         "timestamp":"1558121820000",
         "create_date":"2019-05-17 16:39:00"
        }
 }

Making the deserialization:

// Deserialization
            //Gson gson = new Gson();
            Moedas moedaDia = new Gson().fromJson(response.json, Moedas.class);


            String line = "";

            result.setResult(moedaDia);

Class Coins

    public class Moedas {
        public List<MoedaDia> USD;

        public Moedas( List<MoedaDia> USD) {
            this.USD = USD;
        }



        public List<MoedaDia> getValue() {
            return USD;
        }

        public void setValue(List<MoedaDia> value) {
            this.USD = value;
        }
    }

Moedadia:

    public class MoedaDia {


        public String code; //": "BRL",
        public String codein; //": "BRL",
        public String name; //": "Dólar Comercial",
        public float high; //": "3,9766",
        public float low; //": "3,9748",
        public float varBid;  //": "0,0021",
        public float pctChange;  //: "0,05",
        public float bid; //": "3,9765",
        public float ask; //": "3,9767",
        public String timestamp;  //": "1557873008",
        public String create_date; //": "2019-05-14 21:00:05"


        public MoedaDia(String code, String codein, String name, float high, float low, float varBid, float pctChange, float bid, float ask, String timestamp, String create_date) {
                this.code = code;
                this.codein = codein;
                this.name = name;
                this.high = high;
                this.low = low;
                this.varBid = varBid;
                this.pctChange = pctChange;
                this.bid = bid;
                this.ask = ask;
                this.timestamp = timestamp;
                this.create_date = create_date;
        }
    }
  • I found this error I tried the following Solutions: 1) Turning on/off serializeNulls() in my typeadapater while serializing Certain Fields like Follows: Boolean Current = out.getSerializeNulls(); out.setSerializeNulls(true); out.name("fieldName"). value(someValue); out.setSerializeNulls(false); This didn’t work Unfortunately. 2) Creating two Different Gson’s with serializeNulls setup differently. Then when serializing Choosing between the two gson’s based on what I was serializing. That littered my code and seems inefficient because of the Reflection.

  • Ask the question. Facilitates the reading of others

1 answer

0


SOLVED

Class Coins

public class Moedas {

    public USD USD;

    public com.newsystemsunlimited.com.br.conversordemoedas.entities.USD getUSD() {
        return USD;
    }

    public void setUSD(com.newsystemsunlimited.com.br.conversordemoedas.entities.USD USD) {
        this.USD = USD;
    }
}

Class USD

public class USD {

    public String code; //": "BRL",
    public String codein; //": "BRL",
    public String name; //": "Dólar Comercial",
    public String high; //": "3,9766",
    public String low; //": "3,9748",
    public String varBid;  //": "0,0021",
    public String pctChange;  //: "0,05",
    public String bid; //": "3,9765",
    public String ask; //": "3,9767",
    public String timestamp;  //": "1557873008",
    public String create_date; //": "2019-05-14 21:00:05"



    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getCodein() {
        return codein;
    }

    public void setCodein(String codein) {
        this.codein = codein;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getHigh() {
        return high;
    }

    public void setHigh(String high) {
        this.high = high;
    }

    public String getLow() {
        return low;
    }

    public void setLow(String low) {
        this.low = low;
    }

    public String getVarBid() {
        return varBid;
    }

    public void setVarBid(String varBid) {
        this.varBid = varBid;
    }

    public String getPctChange() {
        return pctChange;
    }

    public void setPctChange(String pctChange) {
        this.pctChange = pctChange;
    }

    public String getBid() {
        return bid;
    }

    public void setBid(String bid) {
        this.bid = bid;
    }

    public String getAsk() {
        return ask;
    }

    public void setAsk(String ask) {
        this.ask = ask;
    }

    public String getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(String timestamp) {
        this.timestamp = timestamp;
    }

    public String getCreate_date() {
        return create_date;
    }

    public void setCreate_date(String create_date) {
        this.create_date = create_date;
    }
}

Browser other questions tagged

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