映射 JSON 对模型的响应

Mapping JSON Response to Model

Rails 映射概念的新手,很想从堆栈溢出社区获得一些帮助。

目前我有来自 Embedly API 的 JSON 响应,我真的不知道如何将它映射到我非常简单的 'posts' 模型。我正在尝试从 JSON 响应中获取 image/title/url 并将其映射到相关字段中的数据库中,因此在 JSON 响应中下面是 'original_url'、'image' 和 'title'。

{
    "provider_url": "http://piccsy.com", 
    "authors": [], 
    "provider_display": "piccsy.com", 
    "related": [], 
    "favicon_url": "http://piccsy.com/favicon.ico", 
    "keywords": [], 
    "app_links": [], 
    "original_url": "http://piccsy.com/2015/02/rihanna-sharks-harpers-bazaar-march-2015-photoshoot3", 
    "media": {}, 
    "content": null, 
    "entities": [], 
    "provider_name": "Piccsy", 
    "type": "html", 
    "description": "Beautiful, inspirational and creative images from Piccsy. Thousands of Piccs from all our streams, for you to browse, enjoy and share with a friend.", 
    "embeds": [], 
    "images": [
        {
            "width": 728, 
            "url": "http://img2.piccsy.com/cache/images/56/8f/bed__396d8_cecf850824130_99f-post.jpg", 
            "height": 1092, 
            "caption": null, 
            "colors": [
                {
                    "color": [
                        190, 
                        211, 
                        212
                    ], 
                    "weight": 0.3095703125
                }, 
                {
                    "color": [
                        114, 
                        159, 
                        171
                    ], 
                    "weight": 0.247314453125
                }, 
                {
                    "color": [
                        0, 
                        52, 
                        68
                    ], 
                    "weight": 0.244140625
                }, 
                {
                    "color": [
                        25, 
                        99, 
                        117
                    ], 
                    "weight": 0.198974609375
                }
            ], 
            "entropy": 5.94797179868, 
            "size": 318918
        }, 
        {
            "width": 200, 
            "url": "http://piccsy.com/piccsy/images/layout/logo/e02f43.200x200.jpg", 
            "height": 200, 
            "caption": null, 
            "colors": [
                {
                    "color": [
                        215, 
                        51, 
                        67
                    ], 
                    "weight": 0.701904296875
                }, 
                {
                    "color": [
                        250, 
                        252, 
                        252
                    ], 
                    "weight": 0.298095703125
                }
            ], 
            "entropy": 0.686638083774, 
            "size": 18691
        }
    ], 
    "safe": true, 
    "offset": null, 
    "cache_age": 86065, 
    "lead": null, 
    "language": null, 
    "url": "http://piccsy.com/2015/02/rihanna-sharks-harpers-bazaar-march-2015-photoshoot3", 
    "title": "Rihanna-sharks-harpers-bazaar-march-2015-photoshoot3", 
    "favicon_colors": [
        {
            "color": [
                208, 
                37, 
                38
            ], 
            "weight": 0.000244140625
        }, 
        {
            "color": [
                0, 
                0, 
                0
            ], 
            "weight": 0.000244140625
        }
    ], 
    "published": null
}

我的帖子模型包含非常简单的名称、url 和图像字段,它们都接受字符串。任何将其映射到模型的帮助都会很棒,到目前为止,我只做了非常简单的 JSON 响应,而这个有点超出我的能力范围。

感谢您的宝贵时间。

您可以只解析 json 响应,并从中获取所需的字段

json_response = '{your json response from api}'
response_hash = JSON.parse(json_response)
MyModel.create!(url: response_hash[:original_url], title: response_hash[:title])

但是图像有问题,响应包含多个图像,所以您可能应该有属于 MyModel 的 ImageModel,以及 MyModel has_many ImageModels。

那么你可以这样做:

model = MyModel.create!(url: response_hash[:original_url], title: response_hash[:title])
response_hash[:images].each do |image|
  model.images.create!(url: image[:url])
end