如何在rails数据库中保存JSON响应的值? [英] How to save values of a JSON response in a rails database?

查看:207
本文介绍了如何在rails数据库中保存JSON响应的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要访问Facebook Graph API并获得如下JSON响应:

  {
data :[
{
name:Coldplay,
category:Musician / band,
id:15253175252,
created_time:2012-01-27T22:51:44 + 0000
},
{
name:REM,
band,
id:8759772983,
created_time:2012-01-27T22:51:43 + 0000
}
] bpaging:{
next:https://graph.facebook.com/1000032123447/music?format=json&limit=5000&offset=5000&__after_id=8123918
}
}



现在我想将名称,类别和ID保存到我的rails数据库。
我有一个具有相同列的表。



将这个响应保存到数据库的匹配列中最简单的方法是什么?或者有更好的解决方案,即只是将整个json对象保存到数据库中,然后再次在代码中读取它?



如果你有时间,请提供一个javascript / jQuery(ajax也许)和ruby代码的编码示例。非常感谢!

解决方案

请参阅这个问题和答案如何将JSON内容从浏览器发送到Rails应用程序。



这样的JSON(将自动解析,并作为 params 散列)。需要注意的是,JSON中的 id 属性将被忽略,因为它受默认保护(Rails使用 id 作为记录的主键)。更好地将其重命名为 facebook_id

 #POST / path / to / collection 
def create
params [data]。each do | data |
data [facebook_id] = data [id]
SomeRecordClass.create(data)
end
end
pre>

当然,您可以进行一些错误检查,发送响应等。



浏览器,如果您使用 jQuery

,则可以执行此类操作

  $。ajax({
type:POST,
url:/ path / to / collection
data:data,// JSON数据,作为对象或字符串
contentType:application / json,
dataType:json
}


I want to access the Facebook Graph API and get JSON responses like this:

{
  "data": [
    {
      "name": "Coldplay", 
      "category": "Musician/band", 
      "id": "15253175252", 
      "created_time": "2012-01-27T22:51:44+0000"
    },
    {
      "name": "R.E.M.", 
      "category": "Musician/band", 
      "id": "8759772983", 
      "created_time": "2012-01-27T22:51:43+0000"
    }
  ], 
  "paging": {
    "next": "https://graph.facebook.com/1000032123447/music?format=json&limit=5000&offset=5000&__after_id=8123918"
  }
}

Now I want to save the name, the category and id into my rails database. I have got a table with the same columns.

What is the easiest way to save this response into the matching columns of the database? Or is there a better solution, i.e. just saving the whole json object into the database and read it later on in the code again?

If you have time, please provide a coding example for the javascript/jQuery (ajax maybe) and ruby code. Thank you so much!

解决方案

See this question and answer for how to send JSON content from the browser to a Rails app.

Then you can do something like this (the JSON will be parsed automatically, and available as the params hash). One thing to note, is that the id attribute in the JSON will be ignored, since it's protected by default (Rails uses id as the primary key for records). Better to "rename it" to something like facebook_id:

# POST /path/to/collection
def create
  params["data"].each do |data|
    data["facebook_id"] = data["id"]
    SomeRecordClass.create(data)
  end
end

Of course you'll want to do some error checking, send a response, etc.

In the browser, you can do something like this, if you're using jQuery

$.ajax({
  type: "POST",
  url: "/path/to/collection",
  data: data, // the JSON data, as an object or string
  contentType: "application/json",
  dataType: "json"
});

这篇关于如何在rails数据库中保存JSON响应的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆