-10

In my project, I am getting the following JSON from the HTTP response.

{"base":"USD","date":"2015-04-24","rates":{"AUD":1.2827,"BGN":1.8069,"BRL":2.9733,"CAD":1.2119,"CHF":0.9551,"CNY":6.1948,"CZK":25.364,"DKK":6.8927,"GBP":0.6614,"HKD":7.75,"HRK":7.0284,"HUF":278.53,"IDR":12954.0,"ILS":3.9244,"INR":63.563,"JPY":119.51,"KRW":1078.25,"MXN":15.358,"MYR":3.5741,"NOK":7.8298,"NZD":1.3216,"PHP":44.281,"PLN":3.7076,"RON":4.08,"RUB":51.215,"SEK":8.6674,"SGD":1.3375,"THB":32.55,"TRY":2.7314,"ZAR":12.182,"EUR":0.9239}}

I want to get the "BGN" from the above json. How to get it.

Marcus
  • 6,697
  • 11
  • 46
  • 89

3 Answers3

0

There are multiple steps involved in this

First: Create a jsonObject

JSONObject jObject = new JSONObject(yourJSON_String);

Second: Get your desired Object or Array, in your case

obj =  getJSONObject("rates")

Third: Get required String

obj.getString("BGN");

Here is the JSON CLASS REFERENCE

Murtaza Khursheed Hussain
  • 15,176
  • 7
  • 58
  • 83
0
JSONObject responseJson = new JSONObject("<your-string">);
JSONObject ratesJson = responseJson.getJSONObject("rates");
double rate = ratesJson.getDouble("BGN");

To see the JSON object clearly, use PrettyJson. Copy paste the JSON string here and it will show you nested objects (if any) for clearly understanding the response.

gsb
  • 5,520
  • 8
  • 49
  • 76
0

Assuming you are using JSONObject, and that your root document is called "doc",

JSONObject rates = doc.getJSONObject("rates") would get you the rates part:

{"AUD":1.2827,"BGN":1.8069,"BRL":2.9733,"CAD":1.2119,"CHF":0.9551,"CNY":6.1948,"CZK":25.364,"DKK":6.8927,"GBP":0.6614,"HKD":7.75,"HRK":7.0284,"HUF":278.53,"IDR":12954.0,"ILS":3.9244,"INR":63.563,"JPY":119.51,"KRW":1078.25,"MXN":15.358,"MYR":3.5741,"NOK":7.8298,"NZD":1.3216,"PHP":44.281,"PLN":3.7076,"RON":4.08,"RUB":51.215,"SEK":8.6674,"SGD":1.3375,"THB":32.55,"TRY":2.7314,"ZAR":12.182,"EUR":0.9239}

From this object you can then simply get the BGN value using

String bgn = rates.getString("BGN")

JHH
  • 8,567
  • 8
  • 47
  • 91