Sunday, November 9, 2008

HowtoGenerateJSON

Recently, I needed JSON format to load data in dhtml component. It was trivial. I installed ruby-json gem. In my model class, I had to do the following:

require 'json/objects'

def to_json
result = Hash.new
rows = []
qg_rows = all_records

qg_rows.each_with_index do |row_data|
row = {}
data = []
row_data.each {|cells|
------
data << cell_data
}
row [:data] = data
rows << row
end
result[:rows] = rows
result.to_json

end

No comments: