Rails,如何循环发布到服务器的JSON对象

时间:2011-04-19 23:45:25

标签: ruby-on-rails json ruby-on-rails-3

我的应用将JSON对象发布回服务器,其结构如下:

contacts:[{"id":38,"full_name":"xxxxxxx","email":""},{"id":70,"full_name":"xxxxxxx","email":""},{"id":79,"full_name":"xxxx","email":""},{"id":22,"full_name":"xxxxxxx","email":""},{"id":48,"full_name":"xxxxxx","email":""},{"id":69,"full_name":"xxxxx","email":""}]

如何在控制器中,我可以遍历每个记录并获取record.id,record.full_name等。

由于

1 个答案:

答案 0 :(得分:2)

您的问题基本上是如何将此JSON字符串序列化为ruby对象(哈希)。您可以致电contacts=JSON.parse(json_string),然后拨打contacts[0].id,等等......