Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 来自深层嵌套哈希和数组的循环特定值_Ruby On Rails_Ruby - Fatal编程技术网

Ruby on rails 来自深层嵌套哈希和数组的循环特定值

Ruby on rails 来自深层嵌套哈希和数组的循环特定值,ruby-on-rails,ruby,Ruby On Rails,Ruby,我是ruby新手,在通过深度嵌套哈希和数组进行循环时遇到困难 假设我有以下JSON: { "Resume":{ .... data .... }, "StructuredXMLResume":{ "ContactInfo":{ .... data .... } ] }, "Employ

我是ruby新手,在通过深度嵌套哈希和数组进行循环时遇到困难

假设我有以下JSON:

{  
   "Resume":{  
               .... data ....
      },
      "StructuredXMLResume":{  
         "ContactInfo":{  
               .... data ....
               }
            ]
         },
         "EmploymentHistory":{  
            "EmployerOrg":[  
               {  
                  "EmployerOrgName":"ABC Corp.",
                  "PositionHistory":[  
                     {  
               .... data ....
                  ]
               },
               {  
                  "EmployerOrgName":"National Geo.",
                  "PositionHistory":[  
                     {  
            .... data ....
                           }
                        ]

                     }
                  ]
               }
            ]
         }

给我ABC公司的

resume.["Resume"]["StructuredXMLResume"]["EmploymentHistory"]["EmployerOrg"][1]["EmployerOrgName"]
给我
国家地理信息。

如何循环打印每个
员工或姓名?

使用:

resume.["Resume"]["StructuredXMLResume"]["EmploymentHistory"]["EmployerOrg"].each do |employer_org|
  puts employer_org["EmployerOrgName"] # or whatever you want to do with the employer_org hash
end
使用:


您可以对任何类似于数组/哈希对象或其行为类似于数组/哈希对象的“对象”使用枚举器(如each),因此在本例中,您希望:

all_employerorgs = resume.["Resume"]["StructuredXMLResume"]["EmploymentHistory"]["EmployerOrg"]
# which is an array of hashes that then you can iterate through 
# you could iterate directly on that but for readibility 
# I would always assign it to a var
all_employerorgs.each do |employerorg|
  puts employerorg['EmployerOrgName']
end

您可以对任何类似于数组/哈希对象或其行为类似于数组/哈希对象的“对象”使用枚举器(如each),因此在本例中,您希望:

all_employerorgs = resume.["Resume"]["StructuredXMLResume"]["EmploymentHistory"]["EmployerOrg"]
# which is an array of hashes that then you can iterate through 
# you could iterate directly on that but for readibility 
# I would always assign it to a var
all_employerorgs.each do |employerorg|
  puts employerorg['EmployerOrgName']
end

我还应该使用
每个
地图
?我对两者之间的区别感到困惑。我还应该使用
每一个
还是
映射
?我对两者的区别感到困惑。