在 Ruby 变量中分配 JSON 的正确方法是什么?

What is the Proper Method of Assigning JSON within a Ruby Variable?

以下 JSON 是一个交易,它将被发送到 Ripple Network 以查询在网关(有点像银行,更像是其客户之间的信托账户)持有加密资产的账户。此脚本将与 PHP 结合使用以获取网关的已发行余额并忽略它的热钱包或日常操作钱包。我的问题是什么是正确的方法:

一个。在 Ruby 变量中分配 JSON?

b。转义双引号和处理 JSON 语法中方括号和方括号出现的换行符的最佳方法是什么?

JSON如下:

ripple_path="/home/rippled/build/rippled" 
conf = "--conf /etc/rippled/rippled.cfg"

puts "About to set the JSON lines "

gatewayStart = "\"method\": \"gateway_balances\","

paramsLine = "\"params\": [ {"

accountLine = "\"account\": \"rGgS5Hw3PhSp3VNT43PDTXze9YfdthHUH\","

hotwalletLine = "\"hotwallet\": \"rKYNhsT3aLymkGH7WL7ZUHkm6RE27iuM4C\","

liLine = "\"ledger_index\": \"validated\","

strictLine = "\"strict\": "

trueLine = true

endLine = " } ] }"

balancesLine = "#{gatewayStart} #{paramsLine} #{accountLine} #>{hotwalletLine} #{liLine} #{strictLine} #{trueLine} #{endLine}"

lineString = "#{balancesLine.to_s}"

linetoJSON = "#{lineString}"

puts "linetoJSON: #{linetoJSON} "

cmd2=`#{ripple_path} #{conf} json gateway_balances #{linetoJSON}`

cmder="#{ripple_path} #{conf} json gateway_balances  #{linetoJSON}"

puts "Done."

输出为:

root@xagate:WorkingDirectory# ruby gatewaybal.rb 

About to set the JSON lines 

linetoJSON: "method": "gateway_balances", "params": [ { "account": 
"rGgS5Hw3PhSp3VNT43PDTXze9YfdthHUH", "hotwallet":         "rKYNhsT3aLymkGH7WL7ZUHkm6RE27iuM4C", "ledger_index": "validated", "strict":rue  } ] } 

Loading: "/etc/rippled/rippled.cfg"

rippled [options] <command> <params>

General Options:

  -h [ --help ]         Display this message. 

.....

Done.

值得注意的是,此命令在通过命令行手动执行时也会 returns 一个 badSyntax 错误。 Please see here 在 ripple 论坛上提出这个问题的镜像。

jsonLine = "'{ \"account\": \"rGgS5Hw3PhSp3VNT43PDTXze9YfdthHUH\", \"hotwallet\": \"rKYNhsT3aLymkGH7WL7ZUHkm6RE27iuM4C\", \"ledger_index\": \"validated\", \"strict\":  true  }'"

是在单个变量中分配此 JSON 的正确方法;此解决方案由 JoelKatz. The completed code is now available on GitHub.

提供