开发者

Saving an object/struct to database as Json

开发者 https://www.devze.com 2023-02-07 14:01 出处:网络
I am using rails and I would like to store a struct in my db. I figured the best way to do this would be to serialize it as json or yaml and then retrieve it, but I am running into some issues. Mostly

I am using rails and I would like to store a struct in my db. I figured the best way to do this would be to serialize it as json or yaml and then retrieve it, but I am running into some issues. Mostly, when I I look at the db the info looks like it's stored fine, but when I try to retrieve it, al开发者_开发百科l of the info is escaped and encoded strangely.

Can someone point me in the right direction in terms of learning about json encoding and storing in a db? Also good ways of retrieving it and running the json?

Thanks so much! Let me know if you would like some examples.


Are you doing JSON.parse(@object.struct_column) while reading data back? For example:

@my_object = MyObject.find(params[:id]))
config     = JSON.parse(@my_object.struct_column)

Also, these two are good reads in that connection.

0

精彩评论

暂无评论...
验证码 换一张
取 消