我有一个看起来像这样的数组.
[{"EntryId"=>"2","Field1"=>"National Life Group","DateCreated"=>"2010-07-30 11:00:14","CreatedBy"=>"tristanoneil"},{"EntryId"=>"3","Field1"=>"Barton Golf Club","DateCreated"=>"2010-07-30 11:11:20","CreatedBy"=>"public"},{"EntryId"=>"4","Field1"=>"PP&D Brochure Distribution",{"EntryId"=>"5","Field1"=>"Prime Renovation Group,DreamMaker Bath & Kitchen","DateCreated"=>"2010-07-30 11:11:21","CreatedBy"=>"public"} ]
我将如何迭代遍历此数组,以便我可以指定要打印的字段并获取值,因此我可以执行类似的操作.
puts EntryId.value
解决方法
大括号和hashrockets(=>)的存在意味着您正在处理Ruby Hash,而不是Array.
幸运的是,检索与任何一个键(hashrocket左边的东西)相关联的值(hashrocket右边的东西)是一块带有Hashes的小菜一碟:你所要做的就是使用[]运算符.
entry = { "EntryId" => "2","Field1" => "National Life Group",... } entry["EntryId"] # returns "2"
以下是Hash:http://ruby-doc.org/core/classes/Hash.html的文档