rails从响应哈希中提取值的最佳方法

时间:2013-07-21 15:31:40

标签: ruby-on-rails json google-image-search

我在调用Google图片搜索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

}

1 个答案:

答案 0 :(得分:1)

你正在寻找这样的东西吗?

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