LoginSignup
4
4

More than 5 years have passed since last update.

[Ruby] Read JSON from url and parse JSON

Last updated at Posted at 2014-10-29

We can use the open-uri to get the content from the url. And use the JSON to parse the JSON string to hash or array.

require 'open-uri'
require 'JSON'

url = "https://api.com/result.json"
json = open(url).read
objs = JSON.parse(json) 
objs.each do |obj|
  title = obj["title"]
end

If the result is not a array. The result will be a dictionary. Just use like this:

obj = JSON.parse(json) 
title = obj["title"]
4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4