Rails,如何循环发送到服务器的 JSON 对象
我的应用程序将一个 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 等。
谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的问题基本上是如何将此 JSON 字符串序列化为 ruby 对象(哈希)。您可以调用
contacts=JSON.parse(json_string)
然后您可以调用contacts[0].id
等...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 callcontacts[0].id
, etc...