Ruby on rails 来自深层嵌套哈希和数组的循环特定值
我是ruby新手,在通过深度嵌套哈希和数组进行循环时遇到困难 假设我有以下JSON:Ruby on rails 来自深层嵌套哈希和数组的循环特定值,ruby-on-rails,ruby,Ruby On Rails,Ruby,我是ruby新手,在通过深度嵌套哈希和数组进行循环时遇到困难 假设我有以下JSON: { "Resume":{ .... data .... }, "StructuredXMLResume":{ "ContactInfo":{ .... data .... } ] }, "Employ
{
"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
我还应该使用每个
或地图
?我对两者之间的区别感到困惑。我还应该使用每一个
还是映射
?我对两者的区别感到困惑。