-2

I have to call 3rd party API for currency exchange rate, but the JSON returned keep changing, if i request a MYR to USD conversion, it will return me: {"MYR_USD":0.246731}, so if i request for KRW to USD, it will return me {"KRW_USD":0.000888}.

I will have to map the returned result into pojo using @JsonProperty, is there any ways that works?

My current hard-coded workaround:

@JsonIgnoreProperties(ignoreUnknown = true)
public class FreeCurrencyExchangeRate {

    @JsonProperty("MYR_USD")
    private double rate;

    public double getRate() {
        return rate;
    }

    public void setRate(double rate) {
        this.rate = rate;
    }
} 
hades
  • 3,200
  • 5
  • 28
  • 56

1 Answers1

1

{"A":5} is an object with field A having value 5.
No {"A":5} is a Map with key/value pair A=5.

Both can be true, but since the value before : is dynamic (changing), it the second interpretation you need.

So don't ask for JSON to be converted to a POJO (FreeCurrencyExchangeRate).
Ask for it to be converted to a Map, then iterate that map.

Andreas
  • 138,167
  • 8
  • 112
  • 195