Here it works, probably you are not applying the method on the right moment, let's try it, first, focusing on the array with something like this:require 'rubygems'require 'active_support/all'class Albums attr_accessor :title, :url def initialize(title, url) @title = title @url = url endendclass Albumlist < Albums attr_accessor :id, :albums def initialize(id) @id = id @albums = Array.new endenda = Albums.new("a", "www.a.com")b = Albums.new("b", "www.b.com")c = Albums.new("c", "www.c.com")list_of_albums_list = Array.newlist_a = Albumlist.new(1)list_b = Albumlist.new(2)list_a.albums << alist_a.albums << blist_b.albums << clist_of_albums_list << list_alist_of_albums_list << list_bputs list_of_albums_list.to_json输出为:[{"albums":[{"title":"a","url":"www.a.com"},{"title":"b","url":"www.b.com"}],"id":1},{"albums":[{"title":"c","url":"www.c.com"}],"id":2}]因此该方法可以按预期工作.therefore the method works as expected. 这篇关于将类的Ruby数组转换为JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-16 07:47