E
E
EvgenySManko2017-02-21 18:28:42
Java
EvgenySManko, 2017-02-21 18:28:42

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

2 answer(s)
A
al_gon, 2017-02-21
@al_gon

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 + "]";
    }
  
  }

E
EvgenySManko, 2017-02-21
@EvgenySManko

Everything turned out to be simple. It was necessary to make a custom deserializer)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question