繁体   English   中英

将JSON响应映射到模型

[英]Mapping JSON Response to Model

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

目前,我有Embedly API的JSON响应,并且我真的不知道如何将其映射到我非常简单的“帖子”模型。 我正在尝试从JSON响应中获取图像/标题/ 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM