问题
Im hard pressed to store data as 'JSON' format into my sqlite database for an rails application. I have searched for how to store data as JSON in my sqlite database but am not seeing many alternatives which are promising. Any one who can guide me on how this can be done?
回答1:
You need to generate a string from your JSON and then save that string in your database as a regular string.
require 'json'
my_hash = {:hello => "goodbye"}
puts JSON.generate(my_hash) => "{\"hello\":\"goodbye\"}"
When you need to use that JSON object, you select your json string and convert it to JSON object using:
json_object = JSON.parse(string)
You can read about JSON objects here: http://ruby-doc.org/stdlib-2.0.0/libdoc/json/rdoc/JSON.html
回答2:
Tried first using 'JSON' as an data type but since SQLite had no support for 'JSON' data type it failed. Then again I performed an migration with the data type for the attribute set up as type 'string' and it started to work.
来源:https://stackoverflow.com/questions/37065966/how-to-store-json-data-in-sqlite