ruby - Working with nested hashes in Rails 3 -


i'm working koala gem , facebook graph api, , want break down results users feed separate variables inserting mysql database, using active record. here code have far:

@token = service.where(:provider => 'facebook', :user_id => session[:user_id]).first.token @graph = koala::facebook::graphapi.new(@token)  @feeds = params[:page] ? @graph.get_page(params[:page]) : @graph.get_connections("me", "home") 

and here @feeds looks like:

    [{"id"=>"1519989351_1799856285747", "from"=>{"name"=>"april daggett swayne", "id"=>"1519989351"}, "picture"=>"http://photos-d.ak.fbcdn.net/hphotos-ak-ash4/270060_1799856805760_1519989351_31482916_3866652_s.jpg", "link"=>"http://www.facebook.com/photo.php?fbid=1799856805760&set=a.1493877356465.2064294.1519989351&type=1", "name"=>"mobile uploads", "icon"=>"http://static.ak.fbcdn.net/rsrc.php/v1/yx/r/og8v99jvf8g.gif", "type"=>"photo", "object_id"=>"1799856805760", "application"=>{"name"=>"facebook android", "id"=>"350685531728"}, "created_time"=>"2011-07-03t03:14:04+0000", "updated_time"=>"2011-07-03t03:14:04+0000"}, {"id"=>"2733058_10100271380562998", "from"=>{"name"=>"joshua ramirez", "id"=>"2733058"}, "message"=>"just posted photo", "picture"=>"http://platform.ak.fbcdn.net/www/app_full_proxy.php?app=124024574287414&v=1&size=z&cksum=228788edbab39cb34861aecd197ff458&src=http%3a%2f%2fimages.instagram.com%2fmedia%2f2011%2f07%2f02%2f2ad9768378cf405fad404b63bf5e2053_7.jpg", "link"=>"http://instagr.am/p/g1tp8/", "name"=>"jtrainexpress's photo", "caption"=>"instagr.am", "icon"=>"http://photos-e.ak.fbcdn.net/photos-ak-snc1/v27562/10/124024574287414/app_2_124024574287414_6936.gif", "actions"=>[{"name"=>"comment", "link"=>"http://www.facebook.com/2733058/posts/10100271380562998"}, {"name"=>"like", "link"=>"http://www.facebook.com/2733058/posts/10100271380562998"}], "type"=>"link", "application"=>{"name"=>"instagram", "id"=>"124024574287414"}, "created_time"=>"2011-07-03t02:07:37+0000", "updated_time"=>"2011-07-03t02:07:37+0000"}, {"id"=>"588368718_10150230423643719", "from"=>{"name"=>"eric bailey", "id"=>"588368718"}, "link"=>"http://www.facebook.com/pages/martis-camp/105474549513998", "name"=>"martis camp", "caption"=>"eric checked in @ martis camp.", "description"=>"rockin pool", "icon"=>"http://www.facebook.com/images/icons/place.png", "actions"=>[{"name"=>"comment", "link"=>"http://www.facebook.com/588368718/posts/10150230423643719"}, {"name"=>"like", "link"=>"http://www.facebook.com/588368718/posts/10150230423643719"}], "place"=>{"id"=>"105474549513998", "name"=>"martis camp", "location"=>{"city"=>"truckee", "state"=>"ca", "country"=>"united states", "latitude"=>39.282813917575, "longitude"=>-120.16736760768}}, "type"=>"checkin", "application"=>{"name"=>"facebook iphone", "id"=>"6628568379"}, "created_time"=>"2011-07-03t01:58:32+0000", "updated_time"=>"2011-07-03t01:58:32+0000", "likes"=>{"data"=>[{"name"=>"mike janes", "id"=>"725535294"}], "count"=>1}}] 

i have looked around clues on this, , haven't found yet (but i'm still working on stackoverflow-foo). appreciated.

that isn't ruby hash, that's fragment of json string. first need decode ruby data structure:

# if json string in json... h = activesupport::json.decode(json) # or favorite json decoder. 

now you'll have hash in h can access other hash:

array = h['data'] puts array[0]['id'] # prints out 1111111111_0000000000000 puts array[0]['from']['name'] # prints jane done 

Comments

Popular posts from this blog

c++ - Is it possible to compile a VST on linux? -

java - Output of Eclipse is rubbish -

jquery - Confused with JSON data and normal data in Django ajax request -