刀不分析PowerShell中的JSONvariables

我试图创build一个亚马逊EC2实例使用“ec2服务器创build”,并通过JSON传递覆盖默认设置通过使用-j选项,其中$ json正确形成JSON文本和我得到相同的错误,如果我引用variables或不:

PS C:\Users\chef> knife ec2 server create --region ... -j $json 

它失败,出现这个错误:

 C:/opscode/chefdk/embedded/lib/ruby/gems/2.1.0/gems/json-1.8.3/lib/json/common.rb:155:in `initialize': A JSON text must at least contain two octets! (JSON::ParserError) from C:/opscode/chefdk/embedded/lib/ruby/gems/2.1.0/gems/json-1.8.3/lib/json/common.rb:155:in `new' from C:/opscode/chefdk/embedded/lib/ruby/gems/2.1.0/gems/json-1.8.3/lib/json/common.rb:155:in `parse' from C:/Users/chef/AppData/Local/chefdk/gem/ruby/2.1.0/gems/knife-ec2-0.12.0/lib/chef/knife/ec2_server_create.rb :218:in `block in <class:Ec2ServerCreate>' 

如果我尝试在命令行上放置JSON,则会得到一个不同的错误:

 C:/opscode/chefdk/embedded/lib/ruby/gems/2.1.0/gems/json-1.8.3/lib/json/common.rb:155:in `parse': 757: unexpected token at '{fusion_**** : *** }' (JSON::ParserError) from C:/opscode/chefdk/embedded/lib/ruby/gems/2.1.0/gems/json-1.8.3/lib/json/common.rb:155:in `parse' from C:/Users/chef/AppData/Local/chefdk/gem/ruby/2.1.0/gems/knife-ec2-0.12.0/lib/chef/knife/ec2_server_create.rb :218:in `block in <class:Ec2ServerCreate>' from C:/opscode/chefdk/embedded/lib/ruby/gems/2.1.0/gems/mixlib-cli-1.5.0/lib/mixlib/cli.rb:235:in `call' 

我一直无法find刀和PowerShell的任何已知的问题,虽然它与最小的JSON的“{}”的工作,它似乎并没有与其他任何工作。

任何人都有这个自己的工作?

shell可能正在吃你的报价,因为这两个错误信息显示他们缺席。

{fusion_**** : *** }是无效的JSON,它肯定需要{"fusion_****" : "***"}键和值引号(假设值是一个string;文字truefalsenull …和数值当然不会被引用)。

我猜想它需要设置这样的东西…

 $json = '{"fusion_****" : "***"}' 

…单引号引起双引号解释为文字。 或这个:

Powershell中的字面转义字符是什么?

…但是,我从字面上不了解Powershell,只是一般的炮弹。 也许在分配variables的地方张贴代码会有帮助,如果这不能帮你解决的话。

我发现的是,你需要用JSON中的引号来反转Ruby的反斜杠。

由于我在我的PowerShellstring中使用了replace,所以我还在PowerShell的string中使用了引号。

例如,要在JSON中得到这个:

  { "fusion": "****" } 

你可以这样编码PowerShell,用单引号(无替代):

  $json = '{ \"fusion_****\" : \"***\" }' 

或者像这样,用双引号(启用replace,但在本例中不使用):

  $json = "{ \`"fusion_****\`" : \`"***\`" }"