在ruby中解析Json响应的问题

时间:2011-06-06 15:47:59

标签: ruby json parsing

  @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 

请查看问题的内联评论。

enter image description here

1 个答案:

答案 0 :(得分:2)

首先,venue['name']是标量,而不是数组;其次,venue['location'](我认为你试图访问)并没有被编码为数组,这只是一个对象:

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

所以你想要:

venue['location']

然后,您的venue['name']['categories'][0]['id']将失败,因为venue['name']是标量;对于这个,你想要:

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