【问题标题】:Jbuilder: How to encode an array of hashes?Jbuilder:如何编码散列数组?
【发布时间】:2013-12-26 16:20:06
【问题描述】:
我想使用 Jbuilder 对以下 JSON 对象进行编码。怎么做?
"should" : [
{
"term" : { "tag" : "wow" }
},
{
"term" : { "tag" : "elasticsearch" }
}
]
【问题讨论】:
标签:
json
elasticsearch
jbuilder
【解决方案1】:
试试child!方法,例如
output = Jbuilder.encode do |json|
json.should do
json.child! do
json.term { json.tag "wow" }
end
json.child! do
json.term { json.tag "elasticsearch" }
end
end
end
puts output
将输出:
{"should":[{"term":{"tag":"wow"}},{"term":{"tag":"elasticsearch"}}]}