使用Jbuilder而不是下划线的json键中的连字符

时间:2017-08-02 20:15:40

标签: ruby-on-rails json jbuilder

我正试图通过jbuilder制作这个json

{
  "query": { 
    "bool": { 
      "must": [
        { "match": { "source-id":2}}
      ]
    }
  }
}

这是我的代码

query = Jbuilder.encode do |json|
                  json.query do
                    json.bool do
                      json.must do
                        json.match do
                          json.source-id source.id
                        end
                      end
                    end
                  end
                end

我收到此错误消息

  

语法错误,意外的tIDENTIFIER,期待keyword_do或'{'或   '('                             json.source-id source.id                                                  ^来自/Users/amir/.rvm/gems/ruby-2.3.0/gems/railties-5.0.2/lib/rails/commands/console.rb:65:in   start' from /Users/amir/.rvm/gems/ruby-2.3.0/gems/railties-5.0.2/lib/rails/commands/console_helper.rb:9:in 从'开始'   /Users/amir/.rvm/gems/ruby-2.3.0/gems/railties-5.0.2/lib/rails/commands/commands_tasks.rb:78:in   console' from /Users/amir/.rvm/gems/ruby-2.3.0/gems/railties-5.0.2/lib/rails/commands/commands_tasks.rb:49:in run_command!”从   /Users/amir/.rvm/gems/ruby-2.3.0/gems/railties-5.0.2/lib/rails/commands.rb:18:in   <top (required)>' from /Users/amir/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.2/lib/active_support/dependencies.rb:293:in需要'来自   /Users/amir/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.2/lib/active_support/dependencies.rb:293:in block in require' from /Users/amir/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.2/lib/active_support/dependencies.rb:259:in load_dependency'来自   /Users/amir/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.2/lib/active_support/dependencies.rb:293:in require' from /Users/amir/source/innovate/self_driving_ideas/bin/rails:9:in'来自   /Users/amir/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.2/lib/active_support/dependencies.rb:287:in load' from /Users/amir/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.2/lib/active_support/dependencies.rb:287:in阻止加载'来自   /Users/amir/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.2/lib/active_support/dependencies.rb:259:in load_dependency' from /Users/amir/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.2/lib/active_support/dependencies.rb:287:in load'来自   /Users/amir/.rvm/gems/ruby-2.3.0/gems/spring-1.7.2/lib/spring/commands/rails.rb:6:in   call' from /Users/amir/.rvm/gems/ruby-2.3.0/gems/spring-1.7.2/lib/spring/command_wrapper.rb:38:in 来电   /Users/amir/.rvm/gems/ruby-2.3.0/gems/spring-1.7.2/lib/spring/application.rb:191:in   block in serve' from /Users/amir/.rvm/gems/ruby-2.3.0/gems/spring-1.7.2/lib/spring/application.rb:161:in 来自   /Users/amir/.rvm/gems/ruby-2.3.0/gems/spring-1.7.2/lib/spring/application.rb:161:in   serve' from /Users/amir/.rvm/gems/ruby-2.3.0/gems/spring-1.7.2/lib/spring/application.rb:131:in 阻止运行'来自   /Users/amir/.rvm/gems/ruby-2.3.0/gems/spring-1.7.2/lib/spring/application.rb:125:in   loop' from /Users/amir/.rvm/gems/ruby-2.3.0/gems/spring-1.7.2/lib/spring/application.rb:125:in 从'跑'   /Users/amir/.rvm/gems/ruby-2.3.0/gems/spring-1.7.2/lib/spring/application/boot.rb:19:in   <top (required)>' from /Users/amir/.rvm/rubies/ruby-2.3.0/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in需要'来自   /Users/amir/.rvm/rubies/ruby-2.3.0/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in`requirement'

1 个答案:

答案 0 :(得分:1)

问题在于使用onTouchEvent(MotionEvent ev)密钥名称中的连字符。如果source-id作为密钥,请尝试执行此操作。

source_id

更新::

否则,您可以执行此操作以获取格式化的密钥:

query = Jbuilder.encode do |json|
              json.query do
                json.bool do
                  json.must do
                    json.match do
                      json.source_id source.id
                    end
                  end
                end
              end
            end

转换所有字符串:

Jbuilder.encode do |json|
  json.key_format!  ->(key) { (key=="source_id") ? "source-id" : key}
  json.query do
    json.bool do
      json.must do
        json.match do
          json.source_id source.id
        end
      end
    end
  end
end

或使用Jbuilder.encode do |json| json.key_format! :dasherize json.query do json.bool do json.must do json.match do json.source_id source.id end end end end end

之类的set!语法