使用Hashie :: Mash Rash美化JSON解析?

时间:2014-07-28 14:32:22

标签: ruby-on-rails ruby json

尝试解析一些丑陋的JSON:

image = product.images.find { |i| i["sizeName"] == "Large" }

如果我使用Hashie::Mash Rash,我可以让它看起来像这样吗?

image = product.images.find { |i| i["size_name"] == "large" }

如果是这样,为什么我会undefined method 'each_pair' for #<Array:0x007f84a0408540>?有关包含实时应用的完整示例,请参阅https://gist.github.com/frankie-loves-jesus/6b8012f9197ca6c675a9

示例JSON:

{
    "metadata": {
        "category": {
            "id": "women",
            "name": "Women's Fashion"
        },
        "showSizeFilter": false,
        "showColorFilter": true,
        "offset": 0,
        "limit": 20,
        "total": 974184
    },
    "products": [
        {
            "id": 377083005,
            "name": "BCBGeneration Women's Contrast Sleeve Trench",
            "currency": "USD",
            "price": 168,
            "priceLabel": "$168.00",
            "salePrice": 106.43,
            "salePriceLabel": "$106.43",
            "inStock": true,
            "retailer": {
                "id": "849",
                "name": "Amazon.com",
                "url": "http://www.shopstyle.com/browse/Amazon.com-US?pid=uid9616-726296-93"
            },
            "locale": "en_US",
            "description": "This jacket features contrasting leather sleeves",
            "brand": {
                "id": "51",
                "name": "BCBG MAX AZRIA",
                "url": "http://www.shopstyle.com/browse/BCBG-MAX-AZRIA?pid=uid9616-726296-93"
            },
            "clickUrl": "http://api.shopstyle.com/action/apiVisitRetailer?id=377083005&pid=uid9616-726296-93",
            "images": [
                {
                    "sizeName": "Small",
                    "width": 32,
                    "height": 40,
                    "url": "http://resources.shopstyle.com/pim/7b/28/7b2894c203529b0956cdd6b760629d4a_small.jpg"
                },
                {
                    "sizeName": "Medium",
                    "width": 112,
                    "height": 140,
                    "url": "http://resources.shopstyle.com/pim/7b/28/7b2894c203529b0956cdd6b760629d4a_medium.jpg"
                },
                {
                    "sizeName": "Large",
                    "width": 164,
                    "height": 205,
                    "url": "http://resources.shopstyle.com/pim/7b/28/7b2894c203529b0956cdd6b760629d4a.jpg"
                }
            ],
            "colors": [
                {
                    "name": "Chino"
                }
            ],
            "sizes": [
                {
                    "name": "XX-Small"
                },
                {
                    "name": "X-Small"
                }
            ],
            "categories": [
                {
                    "id": "raincoats-and-trenchcoats",
                    "name": "Raincoats & Trenchcoats"
                }
            ]
        }
    ]
}

1 个答案:

答案 0 :(得分:1)

这是我的工作代码

require 'json'
require 'rash'

@json_text = <<END
{
    "metadata": {
        "category": {
            "id": "women",
            "name": "Women's Fashion"
        },
        "showSizeFilter": false,
        "showColorFilter": true,
        "offset": 0,
        "limit": 20,
        "total": 974184
    },
    "products": [
        {
            "id": 377083005,
            "name": "BCBGeneration Women's Contrast Sleeve Trench",
            "currency": "USD",
            "price": 168,
            "priceLabel": "$168.00",
            "salePrice": 106.43,
            "salePriceLabel": "$106.43",
            "inStock": true,
            "retailer": {
                "id": "849",
                "name": "Amazon.com",
                "url": "http://www.shopstyle.com/browse/Amazon.com-US?pid=uid9616-726296-93"
            },
            "locale": "en_US",
            "description": "This jacket features contrasting leather sleeves",
            "brand": {
                "id": "51",
                "name": "BCBG MAX AZRIA",
                "url": "http://www.shopstyle.com/browse/BCBG-MAX-AZRIA?pid=uid9616-726296-93"
            },
            "clickUrl": "http://api.shopstyle.com/action/apiVisitRetailer?id=377083005&pid=uid9616-726296-93",
            "images": [
                {
                    "sizeName": "Small",
                    "width": 32,
                    "height": 40,
                    "url": "http://resources.shopstyle.com/pim/7b/28/7b2894c203529b0956cdd6b760629d4a_small.jpg"
                },
                {
                    "sizeName": "Medium",
                    "width": 112,
                    "height": 140,
                    "url": "http://resources.shopstyle.com/pim/7b/28/7b2894c203529b0956cdd6b760629d4a_medium.jpg"
                },
                {
                    "sizeName": "Large",
                    "width": 164,
                    "height": 205,
                    "url": "http://resources.shopstyle.com/pim/7b/28/7b2894c203529b0956cdd6b760629d4a.jpg"
                }
            ],
            "colors": [
                {
                    "name": "Chino"
                }
            ],
            "sizes": [
                {
                    "name": "XX-Small"
                },
                {
                    "name": "X-Small"
                }
            ],
            "categories": [
                {
                    "id": "raincoats-and-trenchcoats",
                    "name": "Raincoats & Trenchcoats"
                }
            ]
        }
    ]
}
END

hash = JSON.parse(@json_text)

@rash = Hashie::Rash.new( hash )


images = []
@rash.products.each do |product|
   images << product.images.find { |i| i.size_name.downcase == "large" }
end


puts images.inspect
#[#<Hashie::Rash height=205 size_name="Large" url="http://resources.shopstyle.com/pim/7b/28/7b2894c203529b0956cdd6b760629d4a.jpg" width=164>]

它不会引起你提到的错误。

我用

$ruby --version
ruby 2.0.0p247 (2013-06-27 revision 41674) [x86_64-darwin13.0.0]
gem list --local |grep 'rash'
rash (0.4.0)
$gem list --local |grep  'hashie'
hashie (3.2.0, 2.0.5)

你能检查一下吗?

并且,如果可能的话,在引发错误时转储json。