Home > Enterprise >  Deserializing json with GSON in Java
Deserializing json with GSON in Java

Time:01-11

I have Json

{"0x3b198e26e473b8fab2085b37978e36c9de5d7f68":{"usd":541.56},"0x54523d5fb56803bac758e8b10b321748a77ae9e9":{"usd":0.059097},"0x330540a9d998442dcbc396165d3ddc5052077bb1":{"usd":1.649e-09}}

Next, I am using gson trying to convert json to price object

        RequestEntity requestEntity = new RequestEntity(requestHeaders, HttpMethod.valueOf("GET"), uri);
        restTemplate.exchange(requestEntity, String.class);
        ResponseEntity<String> responseEntity = restTemplate.exchange(requestEntity, String.class);
        String response = responseEntity.getBody();
        System.out.println(response);

        Gson gson = new Gson();
        Price price = gson.fromJson(response, Price.class);

Price.java

  public class Price {
        private Wallet wallet;
        private Wallet token;
        private Wallet contract;
    
        public Wallet getWallet() {
            return wallet;
        }
    
        public void setWallet(Wallet wallet) {
            this.wallet = wallet;
        }
    
        public Wallet getToken() {
            return token;
        }
    
        public void setToken(Wallet token) {
            this.token = token;
        }
    
        public Wallet getContract() {
            return contract;
        }
    
        public void setContract(Wallet contract) {
            this.contract = contract;
        }
    }

Wallet.java

public class Wallet {
    private Currencies currencies;

    public Currencies getCurrencies() {
        return currencies;
    }

    public void setCurrencies(Currencies currencies) {
        this.currencies = currencies;
    }
}

Currencies.java

public class Currencies {
    String currency;
    Integer value;

    public String getCurrency() {
        return currency;
    }

    public void setCurrency(String currency) {
        this.currency = currency;
    }

    public Integer getValue() {
        return value;
    }

    public void setValue(Integer value) {
        this.value = value;
    }
}

I need to name the class fields "0x3b198e26e473b8fab2085b37978e36c9de5d7f68", "0x54523d5fb56803bac758e8b10b321748a77ae9e9" and "0x330540a9d998442dcbc396165d3dbb150"? If so, these are not valid names.

Otherwise I get null when calling

        System.out.println(price.getWallet());

CodePudding user response:

I hope you can utilize custom deserializers here

register them for Gson like this:

GsonBuilder gsonBldr = new GsonBuilder();
gsonBldr.registerTypeAdapter(Price.class, new PriceCustomDeserializer());
gsonBldr.registerTypeAdapter(Wallet.class, new WalletCustomDeserializer());
gsonBldr.registerTypeAdapter(Currencies.class, new CurrenciesCustomDeserializer());

and deserializers implementation:

public class PriceCustomDeserializer implements JsonDeserializer<Price> {

    @Override
    public Price deserialize
      (JsonElement jElement, Type typeOfT, JsonDeserializationContext context) 
      throws JsonParseException {
        JsonObject jObject = jElement.getAsJsonObject();
        //parse 3 values (without names) from json by order 
    }
}

//add remaining 2 deserializers 
  •  Tags:  
  • Related