Answer the question
In order to leave comments, you need to log in
How to correctly write a JSON string from a request?
Hello.
Tell me please.
There is a GET request, in response to which there is a JSON string. What is needed to correctly write this response into an object - what class, and what deserializer?
Request:
api.fixer.io/latest?callback=?
Thank you very much in advance!
Answer the question
In order to leave comments, you need to log in
For this to be correct JSON, you need a call to api.fixer.io/latest
Take GSON
public static void main(final String[] args) {
final Gson gson = new Gson();
final Response response = gson.fromJson("{base: EUR, date:1, rates:{AUD: 1.3762,BGN: 1.9558}}", Response.class);
System.out.println(response);
}
static class Response {
String base;
String date;
Map<String, String> rates;
@Override
public String toString() {
return "Response [base=" + base + ", date=" + date + ", rates=" + rates + "]";
}
}
Everything turned out to be simple. It was necessary to make a custom deserializer)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question