【问题标题】:hyphen in key for json using Jbuilder instead of underscore使用 Jbuilder 而不是下划线的 json 键中的连字符
【发布时间】:2018-01-10 05:30:44
【问题描述】:

我正在尝试通过 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,需要关键字_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 start' 来自 /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:inrequire' 来自 /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:inload_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:inblock in load' 来自 /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:inload' 来自 /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 call' 来自 /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 fork' 来自 /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 block in run' 来自 /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 run' 来自 /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:inrequire' 来自 /Users/amir/.rvm/rubies/ruby-2.3.0/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'

【问题讨论】:

    标签: ruby-on-rails json jbuilder


    【解决方案1】:

    问题在于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! 语法,如json.set! "source-id", source.id

    【讨论】:

    • 不,我不希望它是下划线,它应该是连字符
    • 你将不得不使用另一种方法来格式化密钥,如果它只是你想要的 hypen 的 source_id 然后硬编码它,就像我展示的那样。否则使用gsub("_", "-") 作为格式化程序
    • 第二个有错误 ArgumentError: wrong number of arguments (given 0, expected 1)
    • 我正在使用字符串化来测试第二个,如下所示,似乎工作正常
    • Jbuilder.encode 做 |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
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-22
    • 2018-05-20
    相关资源
    最近更新 更多