将JSON响应映射到模型

时间:2015-02-19 14:56:58

标签: ruby-on-rails ruby json mapping

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

目前我从Embedly API获得了这个JSON响应,并且真的不知道如何将它映射到我非常简单的帖子'模型。我试图从JSON响应中获取 image / title / url 并将其映射到相关字段中的数据库中,因此在下面的JSON响应中,这些将是' original_url& #39;,' image'和'标题'。

{
    "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
}

我的帖子模型包含非常简单的名称,网址和图片字段,这些字段都是接受字符串。任何有关将此映射到模型的帮助都很棒,到目前为止我只做了非常简单的JSON响应,而且这个有点超出了我的联盟。

感谢您的时间。

1 个答案:

答案 0 :(得分:1)

你可以解析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