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

Posted

技术标签:

【中文标题】使用 Jbuilder 而不是下划线的 json 键中的连字符【英文标题】:hyphen in key for json using Jbuilder instead of underscore 【发布时间】: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'

【问题讨论】:

【参考方案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

以上是关于使用 Jbuilder 而不是下划线的 json 键中的连字符的主要内容,如果未能解决你的问题,请参考以下文章

产生哈希的 JBuilder 循环

使用 Jbuilder(或其他)的 Rails JSON API 布局

在 html 视图中呈现 JBuilder 视图

为啥在测试 RSPEC 时 JBuilder 不返回 JSON 中的响应正文

如何将 JBuilder 视图的 JSON 表示呈现为字符串?

Java,用intelliJ写的,这些红色下划线是啥意思,程序成功运行