dewski / json_builder

Rails provides an excellent XML Builder by default to build RSS and ATOM feeds, but nothing to help you build complex and custom JSON data structures. JSON Builder is here to help.
http://garrettbjerkhoel.com/json_builder/
MIT License
244 stars 52 forks source link

naming for attributes #36

Closed fluxsaas closed 11 years ago

fluxsaas commented 11 years ago

Hey, i just notices that i have some problems with naming like:

array @shifts do |shift|
  id shift.id
  title shift.shift_category.name
  send("end", shift.ends_at)
end

=> title dies not get displayed at all, well "end" is not working but my workaround does :)

dewski commented 11 years ago

I'm not sure I understand what you are saying. I'll be glad to help but I need some background info first.

If I run:

require 'json_builder'

json = JSONBuilder::Compiler.generate do
  @shifts = [{:id => 1, :title => 'test', :ends => Time.now}]
  array @shifts do |shift|
    id shift[:id]
    title shift[:title]
    key :end, shift[:ends]
  end
end

puts json

I will get the expected output:

[{"id": 1, "title": "test", "end": "2012-09-26T23:04:08-07:00"}]
fluxsaas commented 11 years ago

ok, i guess it was too late at night....

  key :end, shift[:ends]

does the trick. i going to check for the "title" key again. not sure why it wasn't working.

thanks for the help!

dewski commented 11 years ago

No problem! Although locally send("end", shift[:ends]) works for me.