Dr Kurubit
Dr Kurubit

Reputation: 13

Show json objects in the view

I need to load a JSON file from a remote link and show the information I want in the view.

In PHP I woud have done it like this:

<?php
$url = "http://www.bitlc.net/stats.json";

$json_stats=json_decode( file_get_contents($url) );
$stats_data = get_object_vars($json_stats);

echo $stats_data[hash_rate];
?>

I searched online but have no idea how to do this.

Upvotes: 1

Views: 1566

Answers (1)

James
James

Reputation: 4807

There are many ways of doing it but here's an example using the Mechanize gem.

json = Mechanize.new.get('http://www.bitlc.net/stats.json').body
result = JSON.parse json

result will be a Ruby hash of the json and you can display it in your views however you'd like.

Upvotes: 2

Related Questions