简体   繁体   English

如何在Rails的ruby中的数据库中存储Json响应

[英]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. 您在这里有很多选择。

  1. 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. 您只能整体阅读。

  2. Parse the response and create individual Tag records in your database. 解析响应并在数据库中创建单个Tag记录。

  3. 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: 您可以按照以下方式进行操作:

  • You add a column to table which you want save that data example "info" column to students table which is text type 您将一列添加到表中要保存该数据示例“信息”列到文本的学生表
  • In Student model, you add this line: 在学生模型中,添加以下行:

     serialize :info, Hash 

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

相关问题 解析JSON以存储在rails数据库中的ruby中 - Parse JSON to store in ruby on rails database 如何解析json response.body以将密钥的值存储在Rails中的ruby中? - How can I parse a json response.body to store a key's value in ruby on rails? 如何在Ruby on Rails中解析文件以存储在数据库中? - How should I parse a file in Ruby on Rails to store in a database? 如何在Rails中将json存储到数据库 - how to store json to database in rails Ruby on Rails:如何使用 JSONPath 访问(并保存到数据库)JSON 数组中的嵌套对象/属性? - Ruby on Rails: How can I use JSONPath to access (and save to database) nested objects/properties within a JSON array? Rails:如何在 Ruby on Rails 迁移中重命名数据库列? - Rails: How can I rename a database column in a Ruby on Rails migration? Ruby on Rails:如何在会话中为authlogic存储其他数据 - Ruby on Rails: how can I store additional data in the session for authlogic 如何在 Rails 中安全地存储 ruby hash? - How can I securely store a ruby hash in Rails? 如何将数据库记录存储到Rails的哈希红宝石中 - How to store database records into hash ruby on rails 如何将数组拆分并存储到Rails中的ruby中的数据库中? - how to split the array and store into the database in ruby on rails?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM