Java - Get the value of a specific element from a JSON

0

I'm using the exchangerate API ( link ) to get the day's currency quotes.

Using the code below I get a JsonObject containing the "rates":

   private JsonObject getExchangesRate() throws JsonIOException, JsonSyntaxException, IOException {

    // Setting URL
    String url_str = "https://v3.exchangerate-api.com/bulk/f75b7f1b080c9060121e6754/BRL";

    // Making Request
    URL url = new URL(url_str);
    HttpURLConnection request = (HttpURLConnection) url.openConnection();
    request.connect();

    // Convert to JSON
    JsonParser jp = new JsonParser();
    JsonElement root = jp.parse(new InputStreamReader((InputStream) request.getContent()));
    JsonObject jsonobj = root.getAsJsonObject();

    // Accessing object
    return jsonobj;

}

Theoretically, it is for him to come in this format:

{
   "result": "success",
   "from": "USD",
   "rates": {
      "AUD": ((AUD in terms of USD)),
      "BGN": 1.8096,
      "BRL": 3.1143,
      "...": 1.3113,
      "...": 7.473, etc. etc.
   }
}

I'm trying to get the values inside the "rates" key and store the USD, EUR and GBP values.

But I can not. I have read about how JSON works in Java but I still can not do what I want.

JsonObject resultadoJSON = getExchangesRate();      

    JsonElement rates = resultadoJSON.get("rates");
    double ValorUSD = (double) rates.get("USD"); //Sei que não é assim, mas é o que to tentando fazer
    double ValorEUR = (double) rates.get("EUR"); //Sei que não é assim, mas é o que to tentando fazer
    
asked by anonymous 21.06.2018 / 16:55

2 answers

1

You can add a cast in the object object as a JsonObject, and after that, pick up the respective values of the converted currencies as JsonElement:

public JsonElement getExchangesRate() throws JsonIOException, JsonSyntaxException, IOException {

        // Setting URL
        String url_str = "https://v3.exchangerate-api.com/bulk/f75b7f1b080c9060121e6754/BRL";

        // Making Request
        URL url = new URL(url_str);
        HttpURLConnection request = (HttpURLConnection) url.openConnection();
        request.connect();

        // Convert to JSON
        JsonParser jp = new JsonParser();
        JsonElement root = jp.parse(new InputStreamReader((InputStream) request.getContent()));
        JsonObject jsonobj = root.getAsJsonObject();
        JsonObject rates = (JsonObject) jsonobj.get("rates");



        // Accessing element
        return rates.get("USD");

    }
    
22.06.2018 / 01:58
0

I was able to resolve it as follows:

JsonObject JSONResult = getExchangesRate(); 
JsonObject rates =  JSONResult.getAsJsonObject("rates");

Double ValorUSD = 1 / rates.get("USD").getAsDouble(); 
Double ValorEUR = 1 / rates.get("EUR").getAsDouble();
Double ValorGBP = 1 / rates.get("GBP").getAsDouble();   //English Pounds

The problem was that the package you were using is GSON (as specified on the API site) and not the normal Java JSON. So the common way was not getting.

    
22.06.2018 / 20:03