如何从API中提取价值

时间:2016-02-07 12:26:46

标签: java android json api android-activity

如何从以下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"}

}

我想专门捕捉CAD购买价值。

目前我的活动上有一个按钮和一个EditText,它根据EditText中的值进行计算,该值由用户手动输入。我想从上面的api中提取值,这样用户就不必手动输入该值。

3 个答案:

答案 0 :(得分:6)

从api

中的responce字符串创建一个JSON对象
JSONObject jsonObject = new JSONObject(response);

然后从中获取CAD对象

JSONObject jsonObjectCad = jsonObject.getJSONObject("CAD");
String buy = jsonObjectCad.getString("buy");

答案 1 :(得分:1)

你所拥有的是一个json字符串。您可以使用Jackson API将此值反映到bean中:

https://github.com/FasterXML/jackson

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

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

网上有很多关于这个主题的例子。

答案 2 :(得分:0)

迟早你需要JSON数据中的所有元素。 试试这段代码,它可以让你收集所有可以根据它的关键值获取的信息

此代码经过测试,也可在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;
    }
}