2011-01-05 18 views
5

非常に簡単な質問です(私は初心者です)。私は、名前とIDを含むFBからのJSONレスポンスを持っている:Rails:JSONから値を抽出する方法

[{"name"=>"John Kline", "id"=>"10276192"}, {"name"=>"Quinn Kumbers", 
"id"=>"18093781"}, {"name"=>"Dan Jacobs", "id"=>"100000918716828"}] ... 

は、私が抽出し、その構造を維持しながら、私のレールのアプリでこのデータにアクセスするにはどうすればよいですか?私はレールに "2番目のエントリーのIDを教えてください"とか、 "私に275番目のエントリー"を伝えたいと思っています。

回答時には何も知らないでください。どうも!他の宝石なし

答えて

8

ActiveSupport::JSON.decode(your_json)

6
# HT Omar Qureshi 
data = ActiveSupport::JSON.decode(your_json) 

# with the id of the 2nd entry 
do_something_with(data[1]['id']) 

# with the 275th entry 
do_something_else_with(data[274]) 

# loop over all the results 
data.each do |datum| 
    puts "#{datum['id']}: #{datum['name']}" 
end 
関連する問題