2

我想使用 Jbuilder 对以下 JSON 对象进行编码。怎么做?

    "should" : [
        {
            "term" : { "tag" : "wow" }
        },
        {
            "term" : { "tag" : "elasticsearch" }
        }
    ]
4

1 回答 1

11

试试孩子!方法,例如

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"}}]}
于 2014-10-10T19:41:58.403 回答