How to use Json

Please Help,

Hi can anybody show me some simple code which shows how using json in
Rails application.

Thanks,
Lekha

lekha p. wrote in post #1106474:

Please Help,

Hi can anybody show me some simple code which shows how using json in
Rails application.

Example 1: Rails bult-in JSON support:

  1. rails generate scaffold User first_name:string last_name:string
  2. rake db:migrate
  3. rails s
  4. Goto: http://localhost:3000/users/new
  5. Create some users
  6. curl http://localhost:3000/users.json

For the code look at app/controllers/users_controller.rb

Example 2: JSON from Array

$ rails console
my_arr = %w{a list of some words in an array}
puts my_arr.to_json

Example 3: JSON from Hash

$ rails console
my_hash = {first: “Hello World”, second: 10, third: Time.now}
puts my_hash.to_json

Example 4: RABL

For more sophisticated JSON needs take a look at examples in the RABL
gem: