使用从 api 中提取的数据难以访问 ruby 中的整个哈希
Difficulty accessing entire hash in ruby with data pulled from api
我目前正在尝试通过 Yelp API 提取数据,但似乎只能访问一半的哈希值。
此代码:
client = Yelp::Client.new
include Yelp::V2::Search::Request
request = Location.new(
:city => 'Melbourne',
:limit => 1) response = client.search(request)
puts response
将输出
的完整哈希值
{"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/pnZSlPiBDl1bS9w7saOAZA/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"}}]}
我可以使用
访问区域信息
puts response["region"]
但我似乎无法访问哈希的其余部分?我特别想提取公司名称。我缺少什么并且需要做什么才能访问整个哈希?
businesses
好像是一个数组。像
response["businesses"][0]["name"]
将检索第一个商家的名称
我目前正在尝试通过 Yelp API 提取数据,但似乎只能访问一半的哈希值。
此代码:
client = Yelp::Client.new
include Yelp::V2::Search::Request
request = Location.new( :city => 'Melbourne', :limit => 1) response = client.search(request)
puts response
将输出
的完整哈希值{"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/pnZSlPiBDl1bS9w7saOAZA/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"}}]}
我可以使用
访问区域信息puts response["region"]
但我似乎无法访问哈希的其余部分?我特别想提取公司名称。我缺少什么并且需要做什么才能访问整个哈希?
businesses
好像是一个数组。像
response["businesses"][0]["name"]
将检索第一个商家的名称