Skip to content

Instantly share code, notes, and snippets.

@fra3il

fra3il/0.rb Secret

Last active January 15, 2016 09:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fra3il/3ba31095d4514b8fa6e6 to your computer and use it in GitHub Desktop.
Save fra3il/3ba31095d4514b8fa6e6 to your computer and use it in GitHub Desktop.
[WP/1438] JSON 을 파싱하는 방법
require "net/http"
require "json"
uri = URI("https://api.spotify.com/v1/search?type=artist&q=tycho")
# get_response 를 이용하는 방법
response = Net::HTTP.get_response(uri)
puts response # <Net::HTTPOK:0x007fbe2b054880>
puts response.body # String
puts JSON.parse(response.body) # Hash
# get 을 이용하는 방법
# response = Net::HTTP.get(uri)
# puts JSON.parse(response) # Hash
require "json"
json = File.read("./slack.json", :external_encoding => "UTF-8") # String
obj = JSON.parse(json) # Hash
obj.each do |key, value|
if key.eql? "results" # Array
value.each do |user|
puts "slack_id : #{user["slack_id"]}, name : #{user["name"]}"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment