skip to Main Content

Please Fix this I m new to android.

I need to be able to edit the value in the @SerializedName annotation with each call. Here’s the JSON response:

{
    "rates": {
        "INR": {  //Problem with INR
            "currency_name": "Indian rupee",
            "rate": "74.4600",
            "rate_for_amount": "74.4600"
        }
    }
}

Here’s my data class:

public class Rates {
        @SerializedName("INR") //Set dynamic serializedName annotation here
        private INR INR;
        public CurrencyConverterResponse.INR getINR() {
            return INR;
        }
    }
    public class INR {
        @SerializedName("currency_name")
        private String currencyName;
        @SerializedName("rate")
        private String rate;
        @SerializedName("rate_for_amount")
        private String rateForAmount;

        public String getCurrencyName() {
            return currencyName;
        }
        public String getRate() {
            return rate;
        }
        public String getRateForAmount() {
            return rateForAmount;
        }
    }

2

Answers


  1. If you have limited currencies, So you use like this in Response class

    public class Rates {
    
        @SerializedName(value = "INR", alternate = {"USD", "GBP", "EUR", "CHF"})
        private Rate rate;
    
        public Rate getRate() {
            return rate;
        }
    }
    
    Login or Signup to reply.
  2. For an unlimited number of options, you should use Map so it will return currency name and rate, this solution will work also if you want to receive more than one rate

    public class Rates {
    
        private Map<String, Rate> rates;
    
        public Map<String, Rate> getRates() {
            return rate;
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search