这应该很简单,但不是
我试图生成一个json,里面有一个循环。像这样的:
all_urls = ["example.com/homepage", "example.com/another", "example.com"]
hash = {
"type":"URLS",
"Params":{
"rules":{
"condition":"AND",
"rules":[
begin
all_urls.each do |url|
{
"condition":"AND",
"rules":[
{
"value":[
"data",
"^https{0,1}:\\/\\/#{url}\\/{0,1}$"
]
},
{
"id":"@timestamp.age",
}
]
}
end
end
]
}
}
}
我想得到:
{
"type":"URLS",
"Params":{
"rules":{
"condition":"AND",
"rules":[
{
"condition":"AND",
"rules":[
{
"value":[
"data",
"^https{0,1}:\\/\\/example.com/homepage\\/{0,1}$"
]
},
{
"id":"@timestamp.age",
}
]
},
{
"condition":"AND",
"rules":[
{
"value":[
"data",
"^https{0,1}:\\/\\/example.com/another\\/{0,1}$"
]
},
{
"id":"@timestamp.age",
}
]
},
{
"condition":"AND",
"rules":[
{
"value":[
"data",
"^https{0,1}:\\/\\/example.com\\/{0,1}$"
]
},
{
"id":"@timestamp.age",
}
]
}
]
}
}
}
试过用地图,试过很多东西要么出错要么把数组扔给我。
编辑:我将提供更清晰的json结构。
更新
使用Damian的建议,我很快就可以让这个工作了,但我只能设法返回数组中的最后一个结果前两个可能被
merge!
覆盖了?见下文:a = ["example.com/homepage", "example.com/another", "example.com"]
hash = {
"type":"condition",
"Params":{
"rules":{
"condition":"AND",
"rules":
(
a.inject(Hash.new) do |hash_container, element|
hash_container.merge!({
"condition":"AND",
"rules":[
{
"value":[
"^https{0,1}:\\/\\/#{element}\\/{0,1}$"
]
},
]
})
end
)
}
}
}
我得到:
=> {:type=>"condition", :Params=>{:rules=>{:condition=>"AND", :rules=>{:condition=>"AND", :rules=>[{:value=>["^https{0,1}:\\/\\/example.com\\/{0,1}$"]}]}}}}
更新3
似乎我的主要错误是假设调用中的“rules”总是一个数组,而实际上,它有时是一个数组,有时是一个对象。因此我感到困惑。
下面的两个答案都是很好的解决方案,最后我使用damian的哈希注入在循环中生成哈希,这是使其工作的关键。
我最终做的是创建“damian”数组,然后使用
#Unshift
在该数组中预处理更多对象,最后在我的调用中添加该数组。 最佳答案
编辑协定编辑问题
你可以在散列中执行一个函数,没有问题,我想问题是你如何使用散列和函数看(我会用reduce代替foreach,因为我更喜欢它,但它是一样的):
all_urls = ["example.com/homepage", "example.com/another", "example.com"]
hash = {
"type":"condition",
"Params":{
"rules":{
"condition":"AND",
"rules":
all_urls.map {|element|
h = Hash.new
h.merge!({
"condition":"AND",
"rules":[
{
"value":[
"^https{0,1}:\\/\\/#{element}\\/{0,1}$"
]
},
]
})
}
}
}
}
结果是:
{: type => "condition",: Params => {: rules => {: condition => "AND",: rules => [{: condition => "AND",
: rules => [{: value => ["^https{0,1}:\\/\\/example.com/homepage\\/{0,1}$"]
}]
}, {: condition => "AND",
: rules => [{: value => ["^https{0,1}:\\/\\/example.com/another\\/{0,1}$"]
}]
}, {: condition => "AND",
: rules => [{: value => ["^https{0,1}:\\/\\/example.com\\/{0,1}$"]
}]
}]
}
}
}
与
each
相同:all_urls = ["example.com/homepage", "example.com/another", "example.com"]
hash = {
"type":"condition",
"Params":{
"rules":{
"condition":"AND",
"rules":
begin
res_lis = []
all_urls.each do |element|
h = Hash.new
h.merge!({
"condition":"AND",
"rules":[
{
"value":[
"^https{0,1}:\\/\\/#{element}\\/{0,1}$"
]
},
]
})
res_lis.push(h)
end
res_lis
end
}
}
}
同样的结果:
{: type => "condition",: Params => {: rules => {: condition => "AND",: rules => [{: condition => "AND",
: rules => [{: value => ["^https{0,1}:\\/\\/example.com/homepage\\/{0,1}$"]
}]
}, {: condition => "AND",
: rules => [{: value => ["^https{0,1}:\\/\\/example.com/another\\/{0,1}$"]
}]
}, {: condition => "AND",
: rules => [{: value => ["^https{0,1}:\\/\\/example.com\\/{0,1}$"]
}]
}]
}
}
}