There're many options to deserialize JSON as mentioned in other answers, most of the time it would be better to define a corresponding Java class and then do serialization/deserialization. One example implementation with Gson would be:
public class Main {
public static void main(String[] args) {
Gson gson = new Gson();
String jsonString = "{\"to\": \"INR\", \"rate\": 64.806700000000006, \"from\": \"USD\"}";
CurrencyRate currencyRate = gson.fromJson(jsonString, CurrencyRate.class);
}
class CurrencyRate {
private String from;
private String to;
private BigDecimal rate;
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
public String getTo() {
return to;
}
public void setTo(String to) {
this.to = to;
}
public BigDecimal getRate() {
return rate;
}
public void setRate(BigDecimal rate) {
this.rate = rate;
}
}
}
And Gson is Thread Safe
, so it's OK to init only one Gson instance and share it among all threads.