4

How do I extract a certain value from the following api?

https://blockchain.info/ticker

{
  "USD" : {"15m" : 376.51, "last" : 376.51, "buy" : 376.79, "sell" : 377.61,  "symbol" : "$"},
  "ISK" : {"15m" : 48027.62, "last" : 48027.62, "buy" : 48063.33, "sell" : 48167.93,  "symbol" : "kr"},
  "HKD" : {"15m" : 2933.63, "last" : 2933.63, "buy" : 2935.81, "sell" : 2942.2,  "symbol" : "$"},
  "TWD" : {"15m" : 12551.49, "last" : 12551.49, "buy" : 12560.82, "sell" : 12588.16,  "symbol" : "NT$"},
  "CHF" : {"15m" : 373.09, "last" : 373.09, "buy" : 373.37, "sell" : 374.18,  "symbol" : "CHF"},
  "EUR" : {"15m" : 337.4, "last" : 337.4, "buy" : 337.65, "sell" : 338.38,  "symbol" : "€"},
  "DKK" : {"15m" : 2517.92, "last" : 2517.92, "buy" : 2519.79, "sell" : 2525.27,  "symbol" : "kr"},
  "CLP" : {"15m" : 265459.51, "last" : 265459.51, "buy" : 265656.92, "sell" : 266235.07,  "symbol" : "$"},
  "CAD" : {"15m" : 523.97, "last" : 523.97, "buy" : 524.36, "sell" : 525.5,  "symbol" : "$"},
  "CNY" : {"15m" : 2475.39, "last" : 2475.39, "buy" : 2477.24, "sell" : 2482.63,  "symbol" : "¥"},
  "THB" : {"15m" : 13398, "last" : 13398, "buy" : 13407.97, "sell" : 13437.15,  "symbol" : "฿"},
  "AUD" : {"15m" : 532.81, "last" : 532.81, "buy" : 533.21, "sell" : 534.37,  "symbol" : "$"},
  "SGD" : {"15m" : 530.11, "last" : 530.11, "buy" : 530.5, "sell" : 531.65,  "symbol" : "$"},
  "KRW" : {"15m" : 453073.94, "last" : 453073.94, "buy" : 453410.87, "sell" : 454397.62,  "symbol" : "₩"},
  "JPY" : {"15m" : 43995.46, "last" : 43995.46, "buy" : 44028.18, "sell" : 44123.99,  "symbol" : "¥"},
  "PLN" : {"15m" : 1487.56, "last" : 1487.56, "buy" : 1488.67, "sell" : 1491.91,  "symbol" : "zł"},
  "GBP" : {"15m" : 259.62, "last" : 259.62, "buy" : 259.82, "sell" : 260.38,  "symbol" : "£"},
  "SEK" : {"15m" : 3182.51, "last" : 3182.51, "buy" : 3184.88, "sell" : 3191.81,  "symbol" : "kr"},
  "NZD" : {"15m" : 568.05, "last" : 568.05, "buy" : 568.47, "sell" : 569.71,  "symbol" : "$"},
  "BRL" : {"15m" : 1469.66, "last" : 1469.66, "buy" : 1470.75, "sell" : 1473.95,  "symbol" : "R$"},
  "RUB" : {"15m" : 29126.34, "last" : 29126.34, "buy" : 29148, "sell" : 29211.43,  "symbol" : "RUB"}

}

I would like to specifically capture the CAD buy value.

Currently I have a button and an EditText on my activity which performs calculations based on the value in EditText which is entered in manually by the user. I would like to extract the value from the api above so the user does not have to manually enter in that value.

oshirowanen
  • 15,297
  • 82
  • 198
  • 350

3 Answers3

6

Create a JSON object from your responce string from the api

JSONObject jsonObject = new JSONObject(response);

then get the CAD object from it

JSONObject jsonObjectCad = jsonObject.getJSONObject("CAD");
String buy = jsonObjectCad.getString("buy");
Avinash Joshi
  • 1,307
  • 8
  • 25
1

What you have is called a json string. You can use Jackson API to reflect this values into beans:

https://github.com/FasterXML/jackson

ObjectMapper mapper = new ObjectMapper();
String jsonInString = ""; // Your string from above.

User user = mapper.readValue(jsonInString, YourClass.class);

There are many examples on this topic in the web.

Marcinek
  • 2,144
  • 1
  • 19
  • 25
0

Sooner or later you will require all the elements in the JSON data. try this code it lets you gather all the information that can be picked up depending on it's key value

this code is tested and also available on Github

private class DownloadData extends AsyncTask<Void,Void, Void>{

    @Override
    protected Void doInBackground(Void... params) {

        try{

            URL url = new URL("https://blockchain.info/ticker");
            HttpsURLConnection httpsURLConnection = (HttpsURLConnection)url.openConnection();
            httpsURLConnection.connect();
            String result = IOUtils.toString(httpsURLConnection.getInputStream());
            System.out.println(""+result);

            HashMap<String, Information> map = new HashMap<>();

            JSONObject jsonObject = new JSONObject(result);
            Iterator<String> iterator = jsonObject.keys();

            while (iterator.hasNext()){
                String key = iterator.next();
                System.out.println(key);

                Information information = new Information();
                information.setA_15m(jsonObject.getJSONObject(key).getString("15m"));
                information.setBuy(jsonObject.getJSONObject(key).getString("last"));
                information.setLast(jsonObject.getJSONObject(key).getString("buy"));
                information.setSell(jsonObject.getJSONObject(key).getString("sell"));
                information.setSymbol(jsonObject.getJSONObject(key).getString("symbol"));
                map.put(key,information);
            }

            Set<String> key_Strings = map.keySet();

            for (String single_key:key_Strings) {
                System.out.println();
                Information information = map.get(single_key);
                System.out.println("Currency: "+single_key);
                System.out.println("15m: "+information.getA_15m());
                System.out.println("Buy: "+information.getBuy());
                System.out.println("Last: "+information.getLast());
                System.out.println("Sell: "+information.getSell());
                System.out.println("Symbol: "+information.getSymbol());
                System.out.println();
            }

        }catch (Exception e){
            e.printStackTrace();
        }            return null;
    }
}

private class Information {

    private String a_15m;
    private String last;
    private String buy;
    private String sell;
    private String symbol;

    public String getA_15m() {
        return a_15m;
    }

    public void setA_15m(String a_15m) {
        this.a_15m = a_15m;
    }

    public String getLast() {
        return last;
    }

    public void setLast(String last) {
        this.last = last;
    }

    public String getBuy() {
        return buy;
    }

    public void setBuy(String buy) {
        this.buy = buy;
    }

    public String getSell() {
        return sell;
    }

    public void setSell(String sell) {
        this.sell = sell;
    }

    public String getSymbol() {
        return symbol;
    }

    public void setSymbol(String symbol) {
        this.symbol = symbol;
    }
}
Pankaj Nimgade
  • 4,529
  • 3
  • 20
  • 30