2013-07-21 70 views
0

我在從調用Google圖片搜索API的響應哈希中提取值時出現問題。rails從響應哈希中提取值的最佳方式

我只想從每個結果的url鍵值。

我想我會呼籲響應deep_symbolize_keys,並完成類似hash.results.url,這是行不通的。我覺得我正在做一些愚蠢的事情,所以任何指針都是值得歡迎的。

控制器代碼

_url = 'http://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=fuzzy%20monkey' 
_response = Net::HTTP.get_response(URI.parse(_url)) 
@response_hash = JSON.parse _response.body 

響應HASH前鍵控存儲在@response_hash

"responseData": { 
    "results": [ 
     { 
      "GsearchResultClass": "GimageSearch", 
      "width": "1152", 
      "height": "864", 
      "imageId": "ANd9GcQQigy-U6KTXke82n5hma5qvFM2UyVnkGtJme6pkZgl_1GYM--Yb90oqnOJ", 
      "tbWidth": "150", 
      "tbHeight": "113", 
      "unescapedUrl": "http://www.blirk.net/wallpapers/1152x864/fuzzy-monkey-1.jpg", 
      "url": "http://www.blirk.net/wallpapers/1152x864/fuzzy-monkey-1.jpg", 
      "visibleUrl": "www.blirk.net", 
      "title": "<b>Fuzzy Monkey</b> Normal 1152x864", 
      "titleNoFormatting": "Fuzzy Monkey Normal 1152x864", 
      "originalContextUrl": "http://www.blirk.net/fuzzy-monkey/1/1152x864/", 
      "content": "<b>Fuzzy Monkey</b> Normal 1152x864", 
      "contentNoFormatting": "Fuzzy Monkey Normal 1152x864", 
      "tbUrl": "http://t1.gstatic.com/images?q=tbn:ANd9GcQQigy-U6KTXke82n5hma5qvFM2UyVnkGtJme6pkZgl_1GYM--Yb90oqnOJ" 
     }, 
     { 
      "GsearchResultClass": "GimageSearch", 
      "width": "600", 
      "height": "397", 
      "imageId": "ANd9GcRpZyXXWBk0TJuU6PCdvrgrU7QckCJQ5DP96iyLc6uLx1bQn4EvBZDFLCk", 
      "tbWidth": "135", 
      "tbHeight": "89", 
      "unescapedUrl": "http://www.acuteaday.com/blog/wp-content/uploads/2011/05/fuzzy-snub-nosed-monkey.jpg", 
      "url": "http://www.acuteaday.com/blog/wp-content/uploads/2011/05/fuzzy-snub-nosed-monkey.jpg", 
      "visibleUrl": "www.acuteaday.com", 
      "title": "<b>Monkeys</b> » A Cute A Day", 
      "titleNoFormatting": "Monkeys » A Cute A Day", 
      "originalContextUrl": "http://www.acuteaday.com/blog/category/monkeys/", 
      "content": "<b>Monkeys</b> » A Cute A Day", 
      "contentNoFormatting": "Monkeys » A Cute A Day", 
      "tbUrl": "http://t1.gstatic.com/images?q=tbn:ANd9GcRpZyXXWBk0TJuU6PCdvrgrU7QckCJQ5DP96iyLc6uLx1bQn4EvBZDFLCk" 
     }, 
     { 
      "GsearchResultClass": "GimageSearch", 
      "width": "800", 
      "height": "600", 
      "imageId": "ANd9GcQGs5BxeWNBIqhM5vL6dVaPcqkopWN8HrqXNdBiuq54HFOXzHBbtgu2wpE", 
      "tbWidth": "143", 
      "tbHeight": "107", 
      "unescapedUrl": "http://wild-facts.com/wp-content/uploads/2010/09/woolly_monkey1.jpg", 
      "url": "http://wild-facts.com/wp-content/uploads/2010/09/woolly_monkey1.jpg", 
      "visibleUrl": "www.wild-facts.com", 
      "title": "Facts about the Woolly <b>Monkey</b> | Wild Facts", 
      "titleNoFormatting": "Facts about the Woolly Monkey | Wild Facts", 
      "originalContextUrl": "http://www.wild-facts.com/2010/wild-fact-723-it-is-not-a-fuzzy-monkey-woolly-monkey/", 
      "content": "Facts about the Woolly <b>Monkey</b> | Wild Facts", 
      "contentNoFormatting": "Facts about the Woolly Monkey | Wild Facts", 
      "tbUrl": "http://t1.gstatic.com/images?q=tbn:ANd9GcQGs5BxeWNBIqhM5vL6dVaPcqkopWN8HrqXNdBiuq54HFOXzHBbtgu2wpE" 
     }, 
     { 
      "GsearchResultClass": "GimageSearch", 
      "width": "1200", 
      "height": "1600", 
      "imageId": "ANd9GcQvIhN2ozrnr6ujdQSbknSV2hexAuA-lP5X22UsDCYzmTsolC97nfjXAVAB", 
      "tbWidth": "113", 
      "tbHeight": "150", 
      "unescapedUrl": "http://4.bp.blogspot.com/_Q6jkicsZAr0/TN1A8bLuGVI/AAAAAAAADoA/NyC1Xl8bNOc/s1600/Fuzzy+Monkey+017.JPG", 
      "url": "http://4.bp.blogspot.com/_Q6jkicsZAr0/TN1A8bLuGVI/AAAAAAAADoA/NyC1Xl8bNOc/s1600/Fuzzy%2BMonkey%2B017.JPG", 
      "visibleUrl": "www.inkingpink.com", 
      "title": "inkingpink: <b>Fuzzy Monkey</b>", 
      "titleNoFormatting": "inkingpink: Fuzzy Monkey", 
      "originalContextUrl": "http://www.inkingpink.com/2010/11/fuzzy-monkey.html", 
      "content": "inkingpink: <b>Fuzzy Monkey</b>", 
      "contentNoFormatting": "inkingpink: Fuzzy Monkey", 
      "tbUrl": "http://t0.gstatic.com/images?q=tbn:ANd9GcQvIhN2ozrnr6ujdQSbknSV2hexAuA-lP5X22UsDCYzmTsolC97nfjXAVAB" 
     } 
    ], 
    "cursor": { 
     "resultCount": "2,990,000", 
     "pages": [ 
      { 
       "start": "0", 
       "label": 1 
      }, 
      { 
       "start": "4", 
       "label": 2 
      }, 
      { 
       "start": "8", 
       "label": 3 
      }, 
      { 
       "start": "12", 
       "label": 4 
      }, 
      { 
       "start": "16", 
       "label": 5 
      }, 
      { 
       "start": "20", 
       "label": 6 
      }, 
      { 
       "start": "24", 
       "label": 7 
      }, 
      { 
       "start": "28", 
       "label": 8 
      } 
     ], 
     "estimatedResultCount": "2990000", 
     "currentPageIndex": 0, 
     "moreResultsUrl": "http://www.google.com/images?oe=utf8&ie=utf8&source=uds&start=0&hl=en&q=fuzzy+monkey", 
     "searchResultTime": "0.21" 
    } 
}, 
"responseDetails": null, 
"responseStatus": 200 

}

回答

1

你在找這樣的事情?

urls = _response["responseData"]["results"].map { |result| result["url"] }