Appending to JSON array in Ruby

来源:互联网 发布:unity3d 画线插件 编辑:程序博客网 时间:2024/05/22 16:49

I am looking to append to a JSON array in ruby. The JSON array looks like this:

{"data" : [{"name":"Chris","long":10,"lat":19}, {"name":"Scott","long":9,"lat":18}]}

I want to be able to append another object to this array e.g

{"name":"John","long":20,"lat":45}

How can I do this?

require 'json'rb_hash = JSON.parse('{"data" : [{"name":"Chris","long":10,"lat":19}, {"name":"Scott","long":9,"lat":18}]}');rb_hash["data"] << { name: "John", long: 20, lat: 45 }rb_hash.to_json

0 0