Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract value from API [duplicate]

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.

like image 426
oshirowanen Avatar asked Feb 07 '16 12:02

oshirowanen


People also ask

What is duplicated value?

A duplicate value is one in which all values in at least one row are identical to all of the values in another row. A comparison of duplicate values depends on the what appears in the cell—not the underlying value stored in the cell.


2 Answers

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");
like image 120
Avinash Joshi Avatar answered Oct 21 '22 09:10

Avinash Joshi


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.

like image 41
Marcinek Avatar answered Oct 21 '22 09:10

Marcinek