我在这个JSON上遇到了麻烦:
{
"ENAX-BRANCHESM-10" :
{
"repo":"test-ASO",
"PATH":"/tmp/pruebaAlvaro",
"ARTIFACTS":"example1.jar,another_one.jar,and_another.jar",
"uri":"http://server:8081/artifactory/test-ASO",
"created":"A705663"
},
"QZQP-QZQPMAN-16" : {
"repo": "test-ASO",
"PATH": "/tmp/pruebaAlvaro2",
"ARTIFACTS": "test543.jar,thisisa.jar,yesanother.jar",
"uri": "http://server:8081/artifactory/test-ASO",
"created": "A705663"
}
}我试图迭代这些列表以获得每个列表的路径和工件值,在示例中有两个列表,但实际上有几十个列表。目的是知道哪个工件将被部署到自己的路径中。例如:
/tmp/pruebaAlvaro
example1.jar
/tmp/pruebaAlvaro
another_one.jar
/tmp/pruebaAlvaro
and_another.jar
/tmp/pruebaAlvaro2
test543.jar
/tmp/pruebaAlvaro2
thisisa.jar
/tmp/pruebaAlvaro2
yesanother.jar经过深入的研究,我还是找不到解决办法。
发布于 2015-09-14 08:20:18
json = JSON.parse(your_json)
values = json.map { |_, v| { v[:PATH] => v[:ARTIFACTS].split(',') } }你会得到美味的哈希
{
'/tmp/pruebaAlvaro' => ['example1.jar', 'another_one.jar', ...],
'/tmp/pruebaAlvaro2' => [...]
}而且,您可以迭代它:
values.each do |path, artifacts|
artifacts.each do |artifact|
puts path
puts artifact
end
puts
end您将得到相同的输出,这是您在问题中提供的
https://stackoverflow.com/questions/32560081
复制相似问题