I'm having problems extracting values in a response hash from a call to google image search API.

I only want the value from the url key from each result.

I figured i'd call deep_symbolize_keys on the response and do something like hash.results.url , which doesn't work. I feel like i'm doing something stupid, so any pointers are welcome.

CONTROLLER CODE

_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

RESPONSE HASH BEFORE KEYED STORED IN @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

}

有帮助吗?

解决方案

are you looking for something like this?

urls = _response["responseData"]["results"].map { |result| result["url"] }
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top