How to communicate with json API

22 views
Skip to first unread message

Reto Bhunjun

unread,
Aug 20, 2015, 3:33:29 PM8/20/15
to rubyonra...@googlegroups.com
Currently I am doing the Ruby on Rails Tutorial from Michael Hartl. I am
developing that code within the cloud9 IDE. What I am interested in
ultimately is to develop some application relating to cryptocurrencies
especially the counterparty protocol. For that, I want to read data from
the blockscan API (google that). There are API requests I want to made
for receiving blockchain data. This data is delivered in json.
Now as a starter, I do not know how to read this json data into a Ruby
variable, do something with it, and render it to a page. Can some one
provide me example code (only the essential lines) that would accomplish
what I wish to do?

Sincerely

--
Posted via http://www.ruby-forum.com/.

Changjin Long

unread,
Aug 21, 2015, 10:14:19 PM8/21/15
to rubyonra...@googlegroups.com
Reto Bhunjun wrote in post #1177949:
After you receive the json data from api server, you can use the JSON
module to parse these data. For example:

```
require 'open-uri'
require 'json'

req =
open('http://xcp.blockscan.com/api2?module=asset&action=list&page=1&count=10')
data = JSON.load(req.read)
```

The var data that return by JSON.load function may be a Ruby Hash or
Array type.
If you want to render it to a page, you can use it in the view and
display it.
Reply all
Reply to author
Forward
0 new messages