From: Brian Candler Date: 2011-02-10T06:01:05+09:00 Subject: Re: Best practice to create a JSON string of one object containing an array of other objects using R And here is a working example for your code where class A includes instances of class B: $ cat ert2.rb require 'rubygems' require 'json' class A attr_accessor :items def initialize() @items = Array.new end def to_json(*a) { "json_class" => self.class.name, "items" => @items, }.to_json(*a) end end class B def to_json(*a) { "json_class" => self.class.name, }.to_json(*a) end end a = A.new a.items << B.new a.items << B.new puts a.to_json $ ruby ert2.rb {"items":[{"json_class":"B"},{"json_class":"B"}],"json_class":"A"} -- Posted via http://www.ruby-forum.com/.