0

我有这个散列,其中有一个数组:

{
  "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
4

2 回答 2

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
于 2013-04-08T19:59:05.277 回答
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"}
于 2013-04-08T20:04:11.013 回答