【问题标题】: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"}}]}
    

    【讨论】:

      猜你喜欢
      • 2023-03-04
      • 1970-01-01
      • 2016-01-21
      • 2023-04-10
      • 2022-12-07
      • 1970-01-01
      • 2014-01-16
      • 2014-07-10
      • 2011-07-26
      相关资源
      最近更新 更多