我有这个哈希,里面有一个数组:
{
"id": "0001001",
"name": "Bob Fisher",
"work": [
{
"employer": {
"id": "6883542487",
"name": "Marvel"
},
"location": {
"id": "108081209214649",
"name": "Las Vegas, Nevada"
},
"position": {
"id": "148835345140954",
"name": "Cirkusdirektör"
},
"description": "API testing",
"start_date": "2012-01",
"end_date": "2013-01"
},
{
"employer": {
"id": "11648563484",
"name": "DC Comics"
},
"location": {
"id": "108424279189115",
"name": "New York, New York"
},
"position": {
"id": "178387758878908",
"name": "Dykare"
},
"description": "Api testing",
"start_date": "2010-02",
"end_date": "2011-01"
}
]
}
如何在ruby中迭代每个雇主的价值观?所以我得到这样的输出:
Employer name: Marvel
Location name: Las Vegas, Nevada
Position: Cirkusdirektör
Description: Api testing
Start-date: 2012-01
End-date: 2013-01
Employer name: DC Comics
Location name: New York, New
Position: Cirkusdirektör
Description: Api testing
Start-date: 2012-01
End-date: 2011-01
答案 0 :(得分:0)
就这么简单:
data['work'].each do |entry|
puts entry['employer']['name']
puts entry['location']['name']
puts entry['position']['name']
puts entry['description']
puts entry['start_date']
puts entry['end_date']
end
答案 1 :(得分:0)
h = {
"id"=> "0001001",
"name"=> "Bob Fisher",
"work"=> [
{
"employer"=> {
"id"=> "6883542487",
"name"=> "Marvel"
},
"location"=> {
"id"=> "108081209214649",
"name"=> "Las Vegas, Nevada"
},
"position"=> {
"id"=> "148835345140954",
"name"=> "Cirkusdirektör"
},
"description"=> "API testing",
"start_date"=> "2012-01",
"end_date"=> "2013-01"
},
{
"employer"=> {
"id"=> "11648563484",
"name"=> "DC Comics"
},
"location"=> {
"id"=> "108424279189115",
"name"=> "New York, New York"
},
"position"=> {
"id"=> "178387758878908",
"name"=> "Dykare"
},
"description"=> "Api testing",
"start_date"=> "2010-02",
"end_date"=> "2011-01"
}
]
}
h.values.last.each do |x|
puts "---------------------------------------"
puts "Employer name:- #{x['employer']['name']}"
puts "Location name:- #{x['location']['name']}"
puts "Position:- #{x['position']['name']}"
puts "Description:- #{x['description']}"
puts "Start-date:- #{x['start_date']}"
puts "End-date: #{x['end_date']}"
puts "---------------------------------------"
end
输出:
---------------------------------------
Employer name:- Marvel
Location name:- Las Vegas, Nevada
Position:- Cirkusdirektör
Description:- API testing
Start-date:- 2012-01
End-date: 2013-01
---------------------------------------
---------------------------------------
Employer name:- DC Comics
Location name:- New York, New York
Position:- Dykare
Description:- Api testing
Start-date:- 2010-02
End-date: 2011-01
---------------------------------------
代码破解:
h.values.last.each do |x|
p x
end
输出:
{"employer"=>{"id"=>"6883542487", "name"=>"Marvel"}, "location"=>{"id"=>"108081209214649", "name"=>"Las Vegas, Nevada"}, "position"=>{"id"=>"148835345140954", "name"=>"Cirkusdirekt\u00F6r"}, "description"=>"API testing", "start_date"=>"2012-01", "end_date"=>"2013-01"}
{"employer"=>{"id"=>"11648563484", "name"=>"DC Comics"}, "location"=>{"id"=>"108424279189115", "name"=>"New York, New York"}, "position"=>{"id"=>"178387758878908", "name"=>"Dykare"}, "description"=>"Api testing", "start_date"=>"2010-02", "end_date"=>"2011-01"}