开发者

Rails, how to loop through a JSON object posted to the server

开发者 https://www.devze.com 2023-02-26 22:28 出处:网络
my app is posting a JSON object back to the server that is structured like so: contacts:[{\"id\":38开发者_运维技巧,\"full_name\":\"xxxxxxx\",\"email\":\"\"},{\"id\":70,\"full_name\":\"xxxxxxx\",\"ema

my app is posting a JSON object back to the server that is structured like so:

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":""}]

How in the controller, can I loop through each of the records and get the record.id, record.full_name etc.

Thanks


Your question is basically how do I serialize this JSON string into a ruby object (hash). You would call contacts=JSON.parse(json_string) you can then call contacts[0].id, etc...

0

精彩评论

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

关注公众号