RubyでJSONをパースする / FBでの自分のお気に入りアーティストの取得

FacebookのGraphAPIの使い方はこの辺りにいろいろ載ってる。
https://developers.facebook.com/docs/reference/api/
例えば音楽をとってくると。。。

{
   "data": [
      {
         "name": "Michael Schenker Group / Temple Of Rock",
         "category": "Musician/band",
         "id": "249184148459561",
         "created_time": "2012-04-25T11:16:13+0000"
      },
      {
         "name": "Sarah Brightman",
         "category": "Musician/band",
         "id": "301956239856782",
         "created_time": "2012-04-10T09:39:05+0000"
      },
      {
         "name": "Boston",
         "category": "Musician/band",
         "id": "259521330754606",
         "created_time": "2012-04-03T04:54:18+0000"
      },
      {
         "name": "Eagles",
         "category": "Musician/band",
         "id": "387465711283000",
         "created_time": "2012-03-11T08:36:43+0000"
      },
      {
...

次はRubyでのJSONの扱い方。
まず、JSONライブラリを読み込む。

require 'json'

parse()メソッドでハッシュに変換できる。
http://d.hatena.ne.jp/snaka72/20080817/1218970157

※参考:
(1)ハッシュの操作法
http://www.rubylife.jp/ini/hash/index8.html
(2)Rubyで型を確認する
http://www.kinopyo.com/blog/to-find-out-ruby-object-class
(a).class:クラスの確認
(b)kind_of?(クラス名):特定のクラスorそのサブクラスのインスタンスかどうか。真偽値。
(c)instance_of?(クラス名):特定のクラスのインスタンスかどうか。真偽値。

# -*- coding: utf-8 -*-                                                         

require 'open-uri'
require 'json'

html = open('https://graph.facebook.com/me/music?access_token=********').read
json = JSON.parser.new(html)
puts json.class
#parse()メソッドでハッシュ生成                                                  
hash =  json.parse()
parsed = hash['data']
parsed.each do |music|
  puts music['name']
end

出力結果

Michael Schenker Group / Temple Of Rock
Sarah Brightman
Boston
Eagles
Styx
Sex Pistols
T.Rex
Nanase Aikawa (相川七瀬)
Poison
UB40 (Official)
The Darkness
Maximum the Hormone
Kraftwerk
Enya
Sonata Arctica
Slayer band
UFO
倉木麻衣
Faith Hill
Beastie Boys
Lady Gaga
Duran Duran
Cindi Lauper
Silly Fools
Simon & Garfunkel
Stevie Wonder
The Clash
Earth, Wind & Fire
The Carpenters
Keith Jarrett
Genesis
Toto
Deodato
Andrew W.K.
Neko Jump
Enigma
Rod Stewart
Sarah Brightman
Simpleplan
Fairuz
Olivia Newton-John
Carole King
Linda Ronstadt
Daft Punk
David Bowie (Official)
Daryl Hall & John Oates
Ah-Ha
Bruce Springsteen
Huey Lewis and the News
Wham!
Ricky Martin
Santana
Slade
The Who
Tower of Power
Steppen Wolf
10cc
Janis Joplin
Yellow Magic Orchestra
The Byrds
The Monkeys
Tokio Hotel
Pleymo
2raumwohnung
Heart
Culture Club
B'z
The Spice Girls
Weezer
Primal Scream
Fountains Of Wayne
Red Hot Chili Peppers
Dewa
Ozzy Osbourne
Devo
WigWam
Danger Danger
Alcatrazz
W.A.S.P.
Anthrax
Sex Machineguns
Dreamtheater
Dream Evil
Nickelback
人間椅子
電気グルーヴ
Pantera
Queen
Guns N' Roses
Motley Crue
Iron Maiden
The Prodigy
Bonjovi
RATT
Metallica
Fatboy Slim
Led Zeppelin Official
Linkin Park
The Rolling Stones
Michael Schenker
Judas Priest
Backstreet Boys
Journey
Boston
Megadeth
The Offspring
Korn
Eminem
Dokken
KISS
Billy Joel
Bob Marley
Anvil
Queensrÿche
Bob Dylan
Grand Funk Railroad
Elton John
The Corrs - Official page
REO Speedwagon
Meja
Bee Gees
Sum 41
ABBA
The Beatles
The Doobie Brothers
The Doors
Foo Fighters
Eric Clapton
Green Day
U2
Sting
The Police
X Japan
Celine Dion
Dragonforce
hide
Dio
Oasis
Rage Against the Machine
Marylin Manson
Mr. Big
Black Sabbath
AC/DC
Aerosmith
Chicago
Avril Lavigne
Gamma Ray
Cheap Trick
Deep Purple
Hinder
Creed
Potato
Seo Tai Ji
Nirvana
King Crimson
The Kinks
Michael Jackson
Radiohead
Limp Bizkit
The Chemical Brothers
Hoobastank
Underworld
Riot
Prince
Van Halen
Madonna
R.E.M.
yes
Frank Zappa
Helloween
ZZ Top
周杰倫 Jay Chou
L.A. Guns
Muse
Lenny Kravitz
System of a Down
Blur
The Jimi Hendrix Experience Official Page
Def Leppard
Accept
Whitesnake
Extreme
Slipknot
Fair Warning
Survivor
Skid Row
Scorpions
Angra
Edguy
Europe

改めて見ると多すぎる。