-1

My data url: http://api.fixer.io/latest?base=USD

{  
   "base":"USD",
   "date":"2017-06-30",
   "rates":{  
      "AUD":1.3013,
      "BGN":1.7138,
      "BRL":3.2948,
      "CAD":1.2956,
      "CHF":0.95776,
      "CNY":6.781,
      "CZK":22.956,
      "DKK":6.5165,
      "GBP":0.77053,
      "HKD":7.8048,
      "HRK":6.4934,
      "HUF":270.74,
      "IDR":13327.0,
      "ILS":3.4953,
      "INR":64.62,
      "JPY":111.94,
      "KRW":1143.2,
      "MXN":18.037,
      "MYR":4.2925,
      "NOK":8.387,
      "NZD":1.363,
      "PHP":50.451,
      "PLN":3.703,
      "RON":3.989,
      "RUB":59.188,
      "SEK":8.4471,
      "SGD":1.3766,
      "THB":33.95,
      "TRY":3.5168,
      "ZAR":13.074,
      "EUR":0.87627
   }
}

I only need the "BGN" rate. Then how can I fetch the data by using Retrofit in Android.

Thanks in advance.

FullStackDeveloper
  • 910
  • 1
  • 16
  • 40

2 Answers2

0
JsonReader jsonReader = Json.createReader(new StringReader("your_string")); 
JsonObject json = jsonReader.readObject();
     JsonObject rates = json.getJsonObject("rates");
        Double no=rates.getJsonNumber("BGN").doubleValue();
Aakash Verma
  • 3,705
  • 5
  • 29
  • 66
0

You can parse like this,

String result = "{ \"base\": \"USD\", \"date\": \"2017-06-30\", \"rates\": { \"AUD\": 1.3013, \"BGN\": 1.7138 } }";

try {
    JSONObject jsonObject = new JSONObject(result);

    JSONObject rateObject = jsonObject.optJSONObject("rates");

    if (rateObject != null) {
        String BGNValue = rateObject.optString("BGN");
    }
} catch (JSONException e) {
    e.printStackTrace();
}
Muthukrishnan Rajendran
  • 11,122
  • 3
  • 31
  • 41