简体   繁体   中英

Difficulty accessing entire hash in ruby with data pulled from api

I'm currently trying to pull data via the Yelp API, and can only seem to access half of the hash.

This code:

client = Yelp::Client.new

include Yelp::V2::Search::Request

request = Location.new( :city => 'Melbourne', :limit => 1) response = client.search(request)

puts response

Will output the full hash of

{"region"=>{"span"=>{"latitude_delta"=>0.0, "longitude_delta"=>0.0}, "center"=>{"latitude"=>28.0772451, "longitude"=>-80.6045478}}, "total"=>2324, "businesses"=>[{"is_claimed"=>false, "rating"=>4.5, "mobile_url"=>" http://m.yelp.com/biz/el-ambia-cubano-melbourne ", "rating_img_url"=>" http://s3-media2.fl.yelpassets.com/assets/2/www/img/99493c12711e/ico/stars/v1/stars_4_half.png ", "review_count"=>168, "name"=>"El Ambia Cubano", "snippet_image_url"=>" http://s3-media1.fl.yelpassets.com/photo/NgfGcZGdYlhTO18p8Shqrw/ms.jpg ", "rating_img_url_small"=>" http://s3-media2.fl.yelpassets.com/assets/2/www/img/a5221e66bc70/ico/stars/v1/stars_small_4_half.png ", "url"=>" http://www.yelp.com/biz/el-ambia-cubano-melbourne ", "phone"=>"3213278389", "snippet_text"=>"4.5 stars to me - rounded up because the kids liked it too.\\n\\nWent here for lunch based mostly on yelp reviews. Rest of my crew voted against Indian or Thai....", "image_url"=>" http://s3-media3.fl.yelpassets.com/bphoto/pnZSlPiBDl1bS9w7sa OAZA/ms.jpg ", "categories"=>[["Cuban", "cuban"]], "display_phone"=>"+1-321-327-8389", "rating_img_url_large"=>" http://s3-media4.fl.yelpassets.com/assets/2/www/img/9f83790ff7f6/ico/stars/v1/stars_large_4_half.png ", "id"=>"el-ambia-cubano-melbourne", "is_closed"=>false, "location"=>{"city"=>"Melbourne", "display_address"=>["950 E Melbourne Ave", "Melbourne, FL 32901"], "geo_accuracy"=>8.0, "postal_code"=>"32901", "country_code"=>"US", "address"=>["950 E Melbourne Ave"], "coordinate"=>{"latitude"=>28.0771809, "longitude"=>-80.6044922}, "state_code"=>"FL"}}]}

I can access the region info by using

puts response["region"]

But I can't seem to access the rest of the hash? I'm specifically trying to extract the business name. What am I missing and need to do to access the entire hash?

businesses seems to be an array. Something like

response["businesses"][0]["name"]

Will retrive the name of the first business

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM