How to store json data in sqlite

后端 未结 2 535
孤独总比滥情好
孤独总比滥情好 2021-01-20 20:51

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

相关标签:
2条回答
  • 2021-01-20 21:15

    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.

    0 讨论(0)
  • 2021-01-20 21:31

    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

    0 讨论(0)
提交回复
热议问题