How to extract value from API [duplicate] How to extract value from API [duplicate] json json

How to extract value from API [duplicate]


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");


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.


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;    }}