将字符串化数组转换回数组 [英] Convert a stringified array back to array

查看:85
本文介绍了将字符串化数组转换回数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将hstore与Postgres 9.2和Rails 3.2一起使用来存储我的对象,如下所示:

I use hstore with Postgres 9.2 and Rails 3.2 to store my object like this:

class User
  user_hstore = {:user_id =>"123", :user_courses => [1,2,3]}
end

现在,当我检索user_courses时,我得到这样的字符串:
'[1、2、3]'

Now, when I retrieve user_courses, I get a string like this: '[1, 2, 3]'

如何将此字符串转换为Rails数组?更好的是,有没有一种方法可以在hstore对象中存储数组,以便Rails可以自动将其作为数组类型检索?

How do I convert this string to Rails array? Better yet, is there a way to store an array within a hstore object so that Rails will automatically retrieve it as array type?

推荐答案

JSON.parse "[\"1018\", \"1037\", \"1045\", \"1042\"]"
#=> ["1018", " 1037", " 1045", " 1042"]

这篇关于将字符串化数组转换回数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆