开发者

Issue in parsing Json response in ruby

开发者 https://www.devze.com 2023-03-10 23:56 出处:网络
@response = Typhoeus::Request.get(FOUR_SQUARE_开发者_运维技巧API_SERVER_ADDRESS+\'search?ll=\' + current_user.altitude.to_s + \"&query=\"+ params[:query] + FOUR_SQUARE_API_ACESS_CODE)
  @response = Typhoeus::Request.get(FOUR_SQUARE_开发者_运维技巧API_SERVER_ADDRESS+'search?ll=' + current_user.altitude.to_s + "&query="+ params[:query] + FOUR_SQUARE_API_ACESS_CODE)
   @venues = ActiveSupport::JSON.decode(@response.body)
   @venues['response']['groups'][0]['items'].each do |venue|
     venue['name']  //working
      venue['name']['location'][0]['address']  //issues
     venue['name']['categories'][0]['id']  //issues
   end 

Please check inline comments for issues.

Issue in parsing Json response in ruby


First of all, the venue['name'] is a scalar, not an array; secondly, venue['location'] (which I think you're trying to access) is not encoded as an array, that's just an object:

location: {
    address: "...',
    city: "...",
    // ...
}

So here you want:

venue['location']

Then, your venue['name']['categories'][0]['id'] will fail because, again, venue['name'] is a scalar; for this one, you want:

venue['categories'][0]['id']
0

精彩评论

暂无评论...
验证码 换一张
取 消