Home > Enterprise >  Set Dynamic SerializedName annotation in android java
Set Dynamic SerializedName annotation in android java

Time:11-16

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;
        }
    }

CodePudding user response:

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;
    }
}
  • Related