0

我在从调用谷歌图像搜索 API 的响应哈希中提取值时遇到问题。

我只想要来自每个结果的 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

存储在@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

}

4

1 回答 1

1

你在找这样的东西吗?

urls = _response["responseData"]["results"].map { |result| result["url"] }
于 2013-07-21T15:46:11.253 回答