给定一个json对象,如何在rails中迭代该对象

时间:2021-11-11 15:26:25

My app is posting the following json object to rails.

我的应用程序正在向rails发布以下json对象。

[{\"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'm unable to loop 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't get the actual id:

但是我不能得到真实的id:

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

That returns nothing. Suggestions on how to get the item ID?

返回什么。如何获得项目ID的建议?

Thanks

谢谢

1 个解决方案

#1


15  

Symbols and strings are different things (except when you're using HashWithIndifferentAccess), you should be using a string as a key:

符号和字符串是不同的东西(除了当你使用hashwithindifference access时),你应该使用字符串作为键:

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

This is a hash with an 'id' key:

这是一个带有“id”键的散列:

{ "id" => 193 }

This is a hash with an :id key:

这是一个散列:id键:

{ :id => 193 }

#1


15  

Symbols and strings are different things (except when you're using HashWithIndifferentAccess), you should be using a string as a key:

符号和字符串是不同的东西(除了当你使用hashwithindifference access时),你应该使用字符串作为键:

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

This is a hash with an 'id' key:

这是一个带有“id”键的散列:

{ "id" => 193 }

This is a hash with an :id key:

这是一个散列:id键:

{ :id => 193 }