[英]How can I store a Json responce in database in ruby on rails
How can I store the json response which I am getting form api into my database. 我如何将正在获取api的json响应存储到数据库中。 Here is what I want to do the responce from API: 这是我想从API进行响应的内容:
[
{
"tag_id": "1",
"tag_name": "FCC",
"group_id": "15",
"object_type_id": "0"
},
{
"tag_id": "2",
"tag_name": "SWA Buyers",
"group_id": "15",
"object_type_id": "0"
},
{
"tag_id": "3",
"tag_name": "SWA Nonbuyers",
"group_id": "15",
"object_type_id": "0"
}
]
Now I want to store all the info in my table 现在我想将所有信息存储在表中
You have plenty of options here. 您在这里有很多选择。
Store this JSON as a string (in a text column). 将此JSON作为字符串存储(在文本列中)。 This obviously won't let you do any queries against the data. 显然,这不允许您对数据进行任何查询。 You can only read it as a whole. 您只能整体阅读。
Parse the response and create individual Tag records in your database. 解析响应并在数据库中创建单个Tag记录。
If your database supports it (postgresql, for example), store it in special json column. 如果您的数据库支持它(例如,PostgreSQL),请将其存储在特殊的json列中。 This is a middle of the previous two options: you get some querying capabilities and ease of saving. 这是前两个选项的中间部分:您可以获得一些查询功能并且易于保存。
You can do it as: 您可以按照以下方式进行操作:
In Student model, you add this line: 在学生模型中,添加以下行:
serialize :info, Hash
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.