有URl要自动存储json数据到数据库
网站提供此代码但使用 Java
HttpResponse response = Unirest.get("https://xproject.restdb.io/rest/data")
.header("x-apikey", "42ba8a6dc7532aa8319a58310281665e394e4")
.header("cache-control", "no-cache")
.asString();
这个json的例子
名为speed 的模型包含speed
和id
需要保存 gps_speed_kph = speed
& id_watch ==_id
最佳答案
首先,您必须将db 列
数据类型指定为您的模型
中的散列
,如here 所述.
然后你必须将你的json
作为散列保存在你的数据库中(json to hash)
:
require 'json'
value = '{"val":"test","val1":"test1","val2":"test2"}'
myhash = JSON.parse(value)
Model.create(hash_column_name: myhash)
为了转换你可以这样做(hash to json)
:
@myhash = Model.find(1).hash_column_name
json_data = @myhash.to_json
关于ruby-on-rails - 如何将json存储到rails中的数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51008503/