5

I'm trying to place the "rates" into a JsonArray using GSON, but it doesn't work, can somebody advise how to get this JSon into a Map, or an array or something of the sort using Gson in Java?

{
  "success": true,
  "timestamp": 1548277447,
  "base": "USD",
  "date": "2019-01-23",
  "rates": {
    "AED": 3.673021,
    "ARS": 37.537301,
    "AUD": 1.400099,
    "BGN": 1.717902,
    "BRL": 3.7657,
    "BWP": 10.52802,
    "CAD": 1.334645,
    "CHF": 0.994703,
    "CLP": 671.898015,
    "CNY": 6.791896,
    "COP": 3151.5,
    "DKK": 6.559594,
    "EGP": 17.891044,
    "EUR": 0.878542,
    "GBP": 0.76529,
    "HKD": 7.84525,
    "HRK": 6.530898,
    "HUF": 279.43017,
    "ILS": 3.673794,
    "INR": 71.13502,
    "ISK": 120.350185,
    "JPY": 109.595496,
    "KRW": 1126.589831,
    "KZT": 378.239562,
    "LKR": 182.190238,
    "LTL": 2.95274,
    "LVL": 0.60489,
    "LYD": 1.390468,
    "MXN": 19.0361,
    "MYR": 4.13696,
    "NOK": 8.56596
  }
}

Edit: Added code

HttpURLConnection fixerConnection = (HttpURLConnection) url.openConnection();
          fixerConnection.setRequestMethod("GET");
          BufferedReader jsonData = new BufferedReader(new InputStreamReader(fixerConnection.getInputStream()));  
          JsonObject allData = new JsonParser().parse(jsonData).getAsJsonObject();           
          JsonArray jArray =  allData.getAsJsonArray("rates");//getAsJsonObject("symbol"); 
2
  • Please share your code that isn't working, as well at the line that gives you the error above. Commented Jan 23, 2019 at 21:48
  • Added, the last line gives me an error. the Json comes from an API and I place it in a buffered reader. Commented Jan 23, 2019 at 21:51

1 Answer 1

5

You will get an Exception for this line here:

JsonArray jArray =  allData.getAsJsonArray("rates");

because as you can see in your input file rates is a JSON Object not an JSON array. Let me show you the difference in syntax :

1. Rates as a JSON Object(As in your case):

"rates": { "AED": 3.673021, "ARS": 37.537301}

2. Rates as a JSON Array:

"rates": [ {"AED": 3.673021}, {"ARS": 37.537301}]

In the 2nd case you can get it as JSON Array of JSON Objects! I suggest reading the DataTypes of JSON W3 Tutorial

Okay now the Solution to extracting all that stuff in your input as JSON Object

HttpURLConnection fixerConnection = (HttpURLConnection) url.openConnection();
          fixerConnection.setRequestMethod("GET");
          BufferedReader jsonData = new BufferedReader(new 
          InputStreamReader(fixerConnection.getInputStream()));  
          JsonObject allData = new JsonParser().parse(jsonData).getAsJsonObject();
          // Now Take Rates as JSON Object and capture it in a Map.
          JsonObject rates =  allData.getAsJsonObject("rates");
          Set<Map.Entry<String, JsonElement>>  entries = rates.entrySet();
Sign up to request clarification or add additional context in comments.

1 Comment

Instead of storing it as a Set<Map.Entry<String, JsonElement>>, it would make more sense to store in a Map<String, JsonElement>.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.