Using json object how to iterate an object in rails

My application sends the next json object to the rails.

[{\"completed\":false,\"id\":196,\"position\":0,\"title\":\"Item 1\",\"updated_at\":\"2011-08-03T21:17:09Z\"},{\"completed\":false,\"id\":193,\"position\":1,\"title\":\"Item X\",\"updated_at\":\"2011-08-03T21:16:19Z\"},{\"completed\":false,\"id\":197,\"position\":2,\"title\":\"Item 2\",\"updated_at\":\"2011-08-03T21:17:11Z\"},{\"completed\":false,\"id\":192,\"position\":3,\"title\":\"Item Z\",\"updated_at\":\"2011-08-03T21:16:17Z\"},{\"completed\":false,\"id\":194,\"position\":4,\"title\":\"Item Y\",\"updated_at\":\"2011-08-03T21:16:21Z\"},{\"completed\":false,\"id\":195,\"position\":5,\"title\":\"Item P\",\"updated_at\":\"2011-08-03T21:16:25Z\"},{\"completed\":false,\"id\":199,\"position\":6,\"title\":\"Why you no stay done?\",\"updated_at\":\"2011-08-03T23:22:41Z\"},{\"completed\":false,\"id\":200,\"position\":7,\"title\":\"Wow\",\"updated_at\":\"2011-08-04T00:02:07Z\"},{\"completed\":false,\"id\":201,\"position\":8,\"title\":\"Hello World\",\"updated_at\":\"2011-08-04T00:02:11Z\"},{\"completed\":false,\"id\":202,\"position\":9,\"title\":\"GOLDEN GATE BRIDGE CATALYST,\",\"updated_at\":\"2011-08-04T00:44:19Z\"}] 

I can not go through the object. I can do:

 JSON.parse(list_items_open).each do |item| Rails.logger.info item.inspect end 

which outputs:

 {"completed"=>false, "id"=>193, "position"=>0, "title"=>"Item X", "updated_at"=>"2011-08-03T21:16:19Z"} {"completed"=>false, "id"=>197, "position"=>1, "title"=>"Item 2", "updated_at"=>"2011-08-03T21:17:11Z"} {"completed"=>false, "id"=>192, "position"=>2, "title"=>"Item Z", "updated_at"=>"2011-08-03T21:16:17Z"} {"completed"=>false, "id"=>196, "position"=>3, "title"=>"Item 1", "updated_at"=>"2011-08-03T21:17:09Z"} {"completed"=>false, "id"=>194, "position"=>4, "title"=>"Item Y", "updated_at"=>"2011-08-03T21:16:21Z"} {"completed"=>false, "id"=>195, "position"=>5, "title"=>"Item P", "updated_at"=>"2011-08-03T21:16:25Z"} {"completed"=>false, "id"=>199, "position"=>6, "title"=>"Why you no stay done?", "updated_at"=>"2011-08-03T23:22:41Z"} {"completed"=>false, "id"=>200, "position"=>7, "title"=>"Wow", "updated_at"=>"2011-08-04T00:02:07Z"} {"completed"=>false, "id"=>201, "position"=>8, "title"=>"Hello World", "updated_at"=>"2011-08-04T00:02:11Z"} {"completed"=>false, "id"=>202, "position"=>9, "title"=>"GOLDEN GATE BRIDGE CATALYST,", "updated_at"=>"2011-08-04T00:44:19Z"} 

But I can not get the actual id:

 JSON.parse(list_items_open).each do |item| Rails.logger.info item[:id] end 

This does not return anything. Suggestions on how to get a product ID?

thanks

+8
json ruby ruby-on-rails ruby-on-rails-3
source share
1 answer

Characters and strings are two different things (except when you use HashWithIndifferentAccess ), you should use the string as a key:

 JSON.parse(list_items_open).each do |item| Rails.logger.info item['id'] end 

This is a hash with the 'id' key:

 { "id" => 193 } 

This is a hash with the key :id :

 { :id => 193 } 
+14
source share

All Articles